Search code examples
buttontkinterswitch-statementframes

Switch between frames in tkinter using functions


From the answer given by Bryan Oakley to the question “Switch between two frames in tkinter”, I trie to change how the button works in page two.

On page one, with command=lambda: controller.show_frame(“StartPage”), it works as it should.

On page two, I want to add something and then go back but it does not work.

Why does my callback not get called?

import tkinter as tk                # python 3
from tkinter import font  as tkfont # python 3

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is the start page", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame("PageOne"))
        button2 = tk.Button(self, text="Go to Page Two",
                            command=lambda: controller.show_frame("PageTwo"))
        button1.pack()
        button2.pack()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 2", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=self.go_to())
        button.pack()
    def go_to(self):
        # Add something to do            
        self.controller.show_frame("StartPage")


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

Solution

  • The problem is that the command keyword argument of Button takes a function. You create your button with tk.Button(self, ..., command=self.go_to()), so the command is self.go_to(), which evaluates to None.

    Why is that so? go_to is defined as follows:

    def go_to(self):
        # Add something to do 
        self.controller.show_frame("StartPage")
    

    So when Python comes across command=self.go_to(), it calls go_to, and passes the returned value to the command option. This value is None (implied by the absence of a return statement), so your command is None, and your button has no effect.

    You need to remove the parentheses, so has to have tk.Button(self, ..., command=self.go_to).