如何在 Tkinter (Python) 中从 children 级别修改 parent 的 `Text`
How to modify `Text` of the parent from the children level in Tkinter (Python)
我得到了一个 app.py
文件,其中调用了主 class App(Tk)
。在其中我创建了三个 children,其中两个是相关的(在图像的中间和右侧):
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
class,然后将此回调传递给 General
class。
app.py
class App(Tk):
def __init__(self):
super().__init__()
self.main = Frame(self)
self.main.pack()
# pass callback to MainModule class
self.active_module = main_module.MainModule(self.main, self.insert_text)
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)
def insert_text(self, text):
self.preview.config(state='normal')
self.preview.insert('end', f'{text}\n')
self.preview.config(state='disabled')
main_module.py
class MainModule(ttk.Notebook):
def __init__(self, parent, callback):
super().__init__(parent)
# pass callback to 'General' class
self.add(General(self, callback), text="General")
class General(ttk.Frame):
def __init__(self, parent, callback):
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)
# call the callback whenever item is selected
self.runtype_combobox.bind('<<ComboboxSelected>>', lambda e: callback(self.runtype_combobox.get()))
我得到了一个 app.py
文件,其中调用了主 class App(Tk)
。在其中我创建了三个 children,其中两个是相关的(在图像的中间和右侧):
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
class,然后将此回调传递给 General
class。
app.py
class App(Tk):
def __init__(self):
super().__init__()
self.main = Frame(self)
self.main.pack()
# pass callback to MainModule class
self.active_module = main_module.MainModule(self.main, self.insert_text)
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)
def insert_text(self, text):
self.preview.config(state='normal')
self.preview.insert('end', f'{text}\n')
self.preview.config(state='disabled')
main_module.py
class MainModule(ttk.Notebook):
def __init__(self, parent, callback):
super().__init__(parent)
# pass callback to 'General' class
self.add(General(self, callback), text="General")
class General(ttk.Frame):
def __init__(self, parent, callback):
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)
# call the callback whenever item is selected
self.runtype_combobox.bind('<<ComboboxSelected>>', lambda e: callback(self.runtype_combobox.get()))