我想将主窗口中的一些用户输入转移到顶级窗口。
到目前为止,我有这个:
import tkinter as tk
from tkinter import filedialog
class Level1 :
def __init__(self, master):
## bunch of frames, labels and button instantiations
...........
self.info = filedialog.askopenfilename()
def get_info(self):
return self.info
def go_to_level2(self):
self.level_2 = tk.Toplevel(self.master)
self.app = Level2(self.level_2)
class Level2:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(......)
self.frame.pack()
self.label1 = tk.Label(self.frame, text = Level1.get_info)
self.label1.pack()
当我这样做时,它打印了内存的实例或位置,例如..牛。。。。有什么想法可以将信息传输到新窗口,还是应该在 level2 中实例化 level1?
编辑:显示未打印(如在tk中。标签(
编辑2:这是解决方案:
import tkinter as tk
from tkinter import filedialog
class Level1 :
def __init__(self, master):
## bunch of frames, labels and button instantiations
...........
self.info = filedialog.askopenfilename()
def get_info(self):
return self.info
def go_to_level2(self):
self.level_2 = tk.Toplevel(self.master)
self.app = Level2(self.level_2, self)
class Level2:
def __init__(self, master, level1):
self.master = master
self.frame = tk.Frame(......)
self.frame.pack()
self.label1 = tk.Label(self.frame, text = level1.get_info())
self.label1.pack()
最常见的解决方案是将包含数据的类实例传递给需要数据的类。
class Level1 :
...
def go_to_level2(self):
...
self.app = Level2(self.level_2, self)
...
class Level2:
def __init__(self, master, level1):
...
self.label1 = tk.Label(self.frame, text = level1.get_info())
...