我需要在一段时间后在窗口之间切换,但我无法让我的代码工作。
我的窗口只有在点击按钮时才会改变。我试着把lambda放在我的命令中,但那仍然没有用。
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()
答案 0 :(得分:0)
您永远不会致电update_Page
。第一步是调用该函数。但是,不需要重复对datetime对象进行计算。只需计算您需要的未来秒数,然后拨打after
一次。