0

Apologies if the title is a bit broad.

I am creating a Tkinter app with multiple pages and I'm using this piece of code in order to do so.

Each page is a frame and frames are raised by calling the 'show_frame' function. I have no problem with switching between pages using buttons, however I want to run a certain function and change pages if a condition is met.

Below is an example:

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()

def doSomething():
    ...
    if x == y:
        "RAISE PAGE ONE"

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

As you can see, the doSomething function is outside of any class. How would I go about raising PageOne from this function.

1 Answers1

0

The easiest answer would be to make doSomething() a method of SampleApp. You would then have access to the method show_frame. However, your question seems to imply this isn't an option.

Failing this, I would advise passing app to the function, and then calling app.show_frame("PageName").

If you don't want to pass a pointer to app as an argument, then you could pass doSomething the function app.show_frame as foo, and then call foo("PageName") when you want the page to be shown.

E.g.

def doSomething(foo):
    '''some code'''
    foo("page name")

doSomething(app.show_frame)
  • It is an option to make it a method of SampleApp. I just thought it was much more common in programming to create a function separately if it wasn't really related to the class. How could I call show_frame within SampleApp? Also, how would I treat the parameters? –  Apr 21 '18 at 18:38
  • show_frame is a method of SampleApp, so to call it you can just write self.show_frame("parameter"). All of your pages seem to have access to the controller (SampleApp), so if doSomething were to be called from them, you could use controller.doSomething(). – random.george 11 Apr 22 '18 at 10:31