0
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?

noG23
  • 93
  • 2
  • 7
  • You're using `tkraise()` incorrectly - it doesn't take a string which happens to be the name of a class that created a widget, it wants *the widget itself*. Normally with this framework you would use the `show_frame()` method rather than using `tkraise()` directly, as it does allow using string to identify the Frame to raise. – jasonharper Dec 24 '21 at 02:38
  • @jasonharper Thanks man, now it works. I changed the `command=lambda: window.tkraise()` to `command=lambda: window.show_frame()` like you suggested. – noG23 Dec 24 '21 at 03:25

0 Answers0