Python tkinter在延迟后显示帧
我想做一个有3页的GUI。起始页有一个标题和一个开始按钮。当按下开始按钮时,将显示第1页,5秒钟后,将显示第2页。 然而,现在它以另一种方式工作:单击按钮时,等待了5秒钟,而起始页保持不变。然后显示了第1页和第2页,我只能看到第2页。Python tkinter在延迟后显示帧,python,tkinter,Python,Tkinter,我想做一个有3页的GUI。起始页有一个标题和一个开始按钮。当按下开始按钮时,将显示第1页,5秒钟后,将显示第2页。 然而,现在它以另一种方式工作:单击按钮时,等待了5秒钟,而起始页保持不变。然后显示了第1页和第2页,我只能看到第2页。 有人能告诉我如何解决这个问题吗?非常感谢 import tkinter as tk import time class MyApp(tk.Tk): def __init__(self, height, width): tk.Tk.__in
有人能告诉我如何解决这个问题吗?非常感谢
import tkinter as tk
import time
class MyApp(tk.Tk):
def __init__(self, height, width):
tk.Tk.__init__(self)
self.geometry('{}x{}'.format(height, width))
self.resizable(width=False, height=False)
self.container = tk.Frame(self)
self.container.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.container.grid_rowconfigure(0, weight=1)
self.container.grid_columnconfigure(0, weight=1)
self.frames = {}
for f in [StartPage, PageOne, PageTwo]:
self.frames[f] = f(self.container, self)
self.frames[f].config(height=height)
self.frames[f].config(width=width)
self.frames[f].grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, page):
self.frames[page].tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.parent = parent
self.controller = controller
self.create_widgets()
def create_widgets(self):
self.instruction = tk.Label(self, text='My Title')
self.instruction.config(font=("Courier", 20))
self.instruction.place(relx=0.5, rely=0.3, anchor=tk.CENTER)
self.button = tk.Button(self, text = 'START', command=self.click_start)
self.button.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
def click_start(self):
# show PageOne
self.controller.show_frame(PageOne)
# wait for 5 seconds
time.sleep(5)
# show PageTwo
self.controller.show_frame(PageTwo)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.parent = parent
self.controller = controller
self.create_widgets()
def create_widgets(self):
self.label = tk.Label(self,text='PageOne')
self.label.config(font=("Courier", 20))
self.label.place(relx=0.4, rely=0.4, anchor=tk.CENTER)
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.parent = parent
self.controller = controller
self.create_widgets()
def create_widgets(self):
self.label = tk.Label(self, text='PageTwo')
self.label.config(font=("Courier", 30))
self.label.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
if __name__ == '__main__':
root = MyApp(height=500, width=500)
root.mainloop()
您可以使用
after
小部件方法来安排未来的事件。第一个参数是毫秒数,下一个参数是对函数的引用,任何附加参数都将传递给函数
def click_start(self):
self.controller.show_frame(PageOne)
self.controller.after(5000, self.controller.show_frame, PageTwo)
请您提供一个最小的、完整的和可验证的示例(),以便我们看到您的问题?我们无法运行您的代码--它取决于名为
BestModel
的类,该类不是您发布的内容的一部分。请仅发布重现问题所需的内容,但请发布重现问题所需的所有内容。