-2

I have a question for the multiple windows in Tkinter (for example: here)

My application with 4 sides is running well. Now I would like to switch from page1 to page3 automatically after 3 seconds is this possible an how can I do this as easy as possible.

Thank you for your help!

import tkinter as tk
from tkinter import font  as tkfont

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

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


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
Popcorn
  • 1
  • 2
  • do some research on the `after` method that is available on all widgets. – Bryan Oakley Apr 27 '18 at 16:28
  • Thanks Bryan Oakley, I have looked so much for the after method, but I can't get a functional code in tkinter. Can you explain how you would like to do this? Maybe with a little example code? – Popcorn May 02 '18 at 17:03
  • `root.after(3000, switch_to_page_3)`. Then, just define a function named `switch_to_page_3` (or give it any name you want) that will switch to page 3. – Bryan Oakley May 02 '18 at 17:20
  • Ok, and what is inside the function. I need the code to switch to page 3. For example: command=lambda: controller.show_frame("page3") this makes no success. That works only in the button widget. – Popcorn May 02 '18 at 17:26
  • I have no idea. It depends on how the rest of your code switches frames. You've shown no code, so all we can do is guess. – Bryan Oakley May 02 '18 at 17:38
  • I edited the mainpost, this is the basic function of my GUI. Now I would like to switch from the StartPage to the PageOne after 3 seconds. With the button "Go to page One" it works fine. – Popcorn May 02 '18 at 17:47

1 Answers1

0

You can use the after method available on all widgets to schedule a function to run in the future. In your case you want to run self.show_frame, so you would do this:

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        ...
        self.show_frame("StartPage")
        self.after(3000, self.show_frame, "PageOne")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685