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.
JavaScript
x
19
19
1
class Tab(ttk.Frame):
2
def __init__(self, master):
3
super().__init__(master)
4
def PrintThings(self):
5
Print(self._A) ##How can I access here self._A to print Hello World
6
7
class App():
8
def __init__(self):
9
self._A = "Hello World"
10
self._root = Tk()
11
self._root.geometry("800x1200")
12
self._TabContainer = ttk.Notebook(self._root)
13
self._Tab = Tab(self._TabContainer)
14
15
def main(self):
16
self._Tab.pack()
17
18
App.main()
19
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:
JavaScript
1
13
13
1
class App():
2
3
self._Tab = Tab(self._TabContainer, app=self)
4
5
6
class Tab(ttk.Frame):
7
def __init__(self, master, app):
8
self.app = app
9
10
11
def PrintThings(self):
12
print(self.app._A)
13