I got an app.py file where the main class App(Tk) is being invoked. Within it I create three children, with two being relevant (in the middle and on the right of the image):
self.active_module = main_module.MainModule(self.main)
self.active_module.grid(column=1,row=0)
and
self.preview = Text(self.main,state='disabled',width=50)
self.preview.grid(column=2,row=0,sticky=E)
The active_module is a ttk.Notebook with several ttk.Frame and separated as a main_module.py file. One of those frames got combobox with different values...
...and the question is how to let preview Text be modified upon changing the combobox value? For clarity purposes I also attach the image how it looks alike:
The relevant parts of code are as follows:
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)
CodePudding user response:
You can pass a callback to MainModule class and then pass this callback to 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()))

