I have a Tkinter App and currently I am trying to split the code from each tab to be on each tab. I can pass the string as a parameter into the Tab but is it possible to access the parent variables from the child ttk.Frame ? The actual code has more things.
class Tab(ttk.Frame): def __init__(self, master): super().__init__(master) def PrintThings(self): Print(self._A) ##How can I access here self._A to print Hello World class App(): def __init__(self): self._A = "Hello World" self._root = Tk() self._root.geometry("800x1200") self._TabContainer = ttk.Notebook(self._root) self._Tab = Tab(self._TabContainer) def main(self): self._Tab.pack() App.main()
Advertisement
Answer
The easiest solution is to pass the instance of App
to each tab. With that, the tab has access to anything defined in App
.
It looks something like this:
class App(): ... self._Tab = Tab(self._TabContainer, app=self) ... ... class Tab(ttk.Frame): def __init__(self, master, app): self.app = app ... def PrintThings(self): print(self.app._A)