【发布时间】:2025-12-17 18:15:02
【问题描述】:
我有一个app.py 文件,其中正在调用主类App(Tk)。在其中我创建了三个孩子,其中两个是相关的(在图像的中间和右侧):
self.active_module = main_module.MainModule(self.main)
self.active_module.grid(column=1,row=0)
和
self.preview = Text(self.main,state='disabled',width=50)
self.preview.grid(column=2,row=0,sticky=E)
active_module 是一个 ttk.Notebook 与多个 ttk.Frame 并分隔为一个 main_module.py 文件。其中一个框架的组合框具有不同的值...
...问题是如何在更改组合框值时修改 preview 文本? 为清楚起见,我还附上了图像:
相关部分代码如下:
app.py
class App(Tk):
def __init__(self):
super().__init__()
[...]
self.active_module = main_module.MainModule(self.main)
self.active_module.grid(column=1,row=0)
self.preview = Text(self.main,state='disabled',width=50)
self.preview.grid(column=2,row=0,sticky=E)
main_module.py
class MainModule(ttk.Notebook):
def __init__(self,parent):
super().__init__(parent)
self.add(General(self),text="General")
class General(ttk.Frame):
def __init__(self,parent):
super().__init__(parent)
self.runtype_label = ttk.Label(self,text="Runtype:")
self.runtype_label.grid(column=0,row=0,sticky=W)
self.runtype_combobox = ttk.Combobox(self,state="readonly",values=("1","2","3"),width=9)
self.runtype_combobox.grid(column=1,row=0,sticky=W)
#self.runtype_combobox.bind('<<ComboboxSelected>>',self.runtype_choice)
【问题讨论】:
-
您可以将回调引用传递给
MainModule,然后将此回调传递给General。 -
我试图通过
self.preview传递__init__和构造函数来做到这一点,但不起作用 -
也许如果你更新你的问题以显示你的代码不起作用和任何错误消息,有人可以帮助修复它。
-
也许您没有在
__init__方法中将父级存储为类变量?self.parent = parent。然后类中的其他方法可以使用对父对象的引用。
标签: python user-interface tkinter