class View(tkinter.Tk):
def __init__(self):
tkinter.Tk.__init__(self)
self.geometry("500x500")
self.title("Switch Frame Example")
container_frame = tkinter.Frame(self)
container_frame.pack(side="top", fill="both", expand=True)
container_frame.grid_rowconfigure(0, weight=1)
container_frame.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (RedFrame, GreenFrame):
frame_name = F.__name__
frame = F(container_frame, self)#container_frame is the parent, self (the window) is the controller
self.frames[frame_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("RedFrame")
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
class RedFrame(tkinter.Frame):
def __init__(self, parent, window):
tkinter.Frame.__init__(self, parent, bg="red", width="500", height="500")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.grid_propagate(0)
goto_green_button = tkinter.Button(self, text='green frame', fg="green", width=25, command=lambda: window.tkraise("GreenFrame"))
goto_green_button.grid(row=0, column=0)
class GreenFrame(tkinter.Frame):
def __init__(self, parent, window):
tkinter.Frame.__init__(self, parent, bg="green", width="500", height="500")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.grid_propagate(0)
goto_red_button = tkinter.Button(self, text='red frame', fg="red", width=25, command=lambda: window.tkraise("RedFrame"))
goto_red_button.grid(row=0, column=0)
The title of this post is the error I get when trying I click the goto_green_button
. I used Bryan Oakley's code taken from this post as the basis for mine, but I'm not sure what I've done wrong, since his code actually works as it should, while mine does not. Why is "GreenFrame" a bad window path? It has the exact same name as the "GreenFrame" class, so shouldn't it work? Or does it have to do with the order of the frames and which is above the other?