your code
this new frame has a pointer to it stored in
when the method
by changing it to
Example
tab_one = tk.Frame(nb) # <-- target actual # #is making a new frame who's parent is the notebook that has a pointer to it in the variable
nb
.this new frame has a pointer to it stored in
tab_one
which is a local variable to the method build_tab
.when the method
build_tab
has done all its code, tab_one
will be gone leaving no access to the frame.by changing it to
self.tab_one = tk.Frame(nb) # <-- target actual # #
tab_one
becomes a instance variable, that is accessible from both inside the instance(by other methods) and outside the instance(by the object instance).Example
class MainFrame: def __init__(self): self.build_nb() def build_nb(self): tab_one = '<local variable>)' self.tab_one = '<instance variable>' print('printed by MainFrame>build_nb:\n' f'tab_one = {tab_one}\n' f'self.tab_one = {self.tab_one}\n') def output_tab_one(self): print('printed by MainFrame>output_tab_one:\n' f'tab_one = tab_one cannot be accessed its forgotten about\n' f'self.tab_one = {self.tab_one}\n') main_frame = MainFrame() main_frame.output_tab_one() print('printed by instance of MainFrame:\n' f'tab_one = tab_one cannot be accessed its forgotten about\n' f'self.tab_one = {main_frame.tab_one}')
Output:printed by MainFrame>build_nb:
tab_one = <local variable>)
self.tab_one = <instance variable>
printed by MainFrame>output_tab_one:
tab_one = tab_one cannot be accessed its forgotten about
self.tab_one = <instance variable>
printed by instance of MainFrame:
tab_one = tab_one cannot be accessed its forgotten about
self.tab_one = <instance variable>