Change Windows Tkinter after some time

Diego Lelis diego.o.lelis at gmail.com
Sun Apr 10 09:36:41 EDT 2016


I need to make a change between windows, after some time. But i am have a little bit of trouble making my code work: My windows change only when i click on button, i tried to put lambda in my command and also don't work.

import tkinter as tk   # python3
#import Tkinter as tk   # python
import datetime
TITLE_FONT = ("Helvetica", 18, "bold")
time_start = datetime.datetime.now()
delta_time = datetime.timedelta(seconds = 5)
class SampleApp(tk.Tk):

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

        # 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, TimePage):
            page_name = F.__name__
            frame = F(container, 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=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Time Page",
                            command=lambda: controller.show_frame(self.start_Counting()) )
        button1.pack()

    def start_Counting(self):
        global time_start
        time_start = datetime.datetime.now()
        return 'TimePage'

class TimePage(tk.Frame):

    def __init__(self, parent, controller):
        global delta_time
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is Time Page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        self.time_exit = tk.StringVar()
        self.time_exit.set('%s' %datetime.datetime.now())
        label2 = tk.Label(self, text=self.time_exit, font=TITLE_FONT)
        label2.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 update_Page(self):
        global time_start, delta_time
        #print('Executou o atualizar')
        time_until = delta_time - (datetime.datetime.now() - time_start)
        self.time_exit.set('%s' %time_until)
        if time_until <= 0:
            self.controller.show_frame('StartPage') # Go to the start_page after 5 seconds
        self.after(1000, update_Page)

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



More information about the Python-list mailing list