Reputation: 297
Without any formal training and a very basic understanding of Python 3.3, I am trying really hard to understand tkinter to make a basic gui. I have read up on this method:
Tkinter! Understanding how to switch frames
which is way over my head. I don't learn very well from copying and pasting code. My question is this:
If I want to make a gui with multiple windows, which I achieve by simply destroying a frame and replacing it with another while using a class, I guess I just don't understand why it's so darn difficult. I played around for a while today and I don't understand why the following code doesn't work. What I cannot wrap my noodle around is why does it flip to the new window properly, but fails to return to the main window. Both buttons are coded identically for their respective purposes, yet one works and one doesn't.
from tkinter import *
class Application:
def __init__(self, master):
self.master = master
self.new_switch_on = False
self.main_switch_on = False
self.main_frame()
def main_frame(self):
if self.new_switch_on == True:
self.new_frame.destroy()
self.new_switch_on = False
self.main_switch_on = True
self.main_frame = Frame(self.master, width = 200, height = 100)
Label(self.main_frame, text = 'Main Frame').pack()
Button(self.main_frame, text = 'Next', command = self.new_frame).pack()
self.main_frame.pack()
def new_frame(self):
if self.main_switch_on == True:
self.main_frame.destroy()
self.main_switch_on = False
self.new_switch_on = True
self.new_frame = Frame(self.master, width = 400, height = 200)
Label(self.new_frame, text = 'New Frame').pack()
Button(self.new_frame, text = 'Back', command = self.main_frame).pack()
self.new_frame.pack()
root = Tk()
root.geometry('-1+1')
app = Application(root)
root.mainloop()
I know I can follow the aforementioned link to get the desired result, but I just don't understand why something like this, or something else that is a bit more simplistic for a new learner couldn't accomplish the same result.
Upvotes: 0
Views: 127
Reputation: 142641
Problem is because you use the same name for method main_frame(self, ...)
and for frame self.main_frame = Frame(...)
.
When you assign frame to variable self.main_frame = Frame(...)
then you loose access to method self.main_frame()
and your Button(self.new_frame, text = 'Back', command=self.main_frame)
can't call method self.main_frame
.
Use different name for method main_frame
- for example create_main_frame
The same problem is with new_frame(self, ...)
and self.new_frame = Frame(...)
(ps. I add to code empty lines to make it more readable)
from tkinter import *
class Application:
def __init__(self, master):
self.master = master
self.new_switch_on = False
self.main_switch_on = False
self.create_main_frame()
def create_main_frame(self): # new name
if self.new_switch_on == True:
self.new_frame.destroy()
self.new_switch_on = False
self.main_switch_on = True
self.main_frame = Frame(self.master, width = 200, height = 100)
Label(self.main_frame, text = 'Main Frame').pack()
Button(self.main_frame, text = 'Next', command = self.create_new_frame).pack() # new name
self.main_frame.pack()
def create_new_frame(self): # new name
if self.main_switch_on == True:
self.main_frame.destroy()
self.main_switch_on = False
self.new_switch_on = True
self.new_frame = Frame(self.master, width = 400, height = 200)
Label(self.new_frame, text='New Frame').pack()
Button(self.new_frame, text='Back', command=self.create_main_frame).pack() # new name
self.new_frame.pack()
root = Tk()
root.geometry('-1+1')
app = Application(root)
root.mainloop()
Upvotes: 1