python - 使用 Tkinter 延迟后更改页面
问题描述
延迟后无法更改帧
如何在下面提供的代码中使用 logoPage 类中的 showFrames 方法?
class temp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "SOC_Workups")
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 (logoPage, mainPage):
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.showFrame("logoPage")
def showFrame(self, page):
frame = self.frames[page]
frame.tkraise()
class logoPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
#label.pack(pady = 10, padx = 10)
canvas = tk.Canvas(self, height=500, width=1000)
canvas.pack()
logo = tk.PhotoImage(file='logo1.png')
background_label = tk.Label(self, image=logo)
background_label.image = logo
background_label.place(relwidth=1, relheight=1)
#parent.showFrame("mainPage")
class mainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
canvas = tk.Canvas(self, height=500, width=1000)
canvas.pack()
app = temp()
app.mainloop()
该应用程序应以 logoPage 开头,然后在 10 秒后更改为 mainPage。我无法从 logoPage 类调用 showFrame 方法。
解决方案
您有权showPage()
使用controller
controller.showFrame("mainPage")
要延迟运行,您可以使用root.after(millisecond, callback)
. 在您的代码中,它需要lambda
创建使用参数运行函数的回调
parent.after(10000, lambda:controller.showFrame("mainPage"))
编辑:正如 Bryan Oakley 在评论中所说,你可以在没有的情况下运行它lambda
parent.after(10000, controller.showFrame, "mainPage")
中的第二个参数after()
是callback
- 没有的函数名称()
- 并且它的所有参数都作为下一个参数after()
我也在下面的代码中更改了它。
完整的工作代码:
PEP 8 - Style Guide for Python Code中有一个很好的规则,建议CamelCaseNames
用于类 - 即。LogoPage
, MainPage
,Temp
类似于Button
, Frame
,Button
等。它有助于识别代码中的类。一些工具对类使用特殊颜色 - 请参阅下面代码中的颜色。
import tkinter as tk
class Temp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "SOC_Workups")
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 (LogoPage, MainPage):
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")
print(self.frames)
self.showFrame("LogoPage")
def showFrame(self, page):
frame = self.frames[page]
frame.tkraise()
class LogoPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
#label.pack(pady = 10, padx = 10)
canvas = tk.Canvas(self, height=500, width=1000)
canvas.pack()
logo = tk.PhotoImage(file='logo1.png')
background_label = tk.Label(self, image=logo)
background_label.image = logo
background_label.place(relwidth=1, relheight=1)
#parent.showFrame("mainPage")
#parent.after(10000, lambda:controller.showFrame("MainPage"))
parent.after(10000, controller.showFrame, "MainPage")
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
canvas = tk.Canvas(self, height=500, width=1000)
canvas.pack()
app = Temp()
app.mainloop()
推荐阅读
- vba - 搜索大量文件并根据搜索字符串将某些行提取到单独的单个文件中
- html - 找出 CSS 行为背后原因的方法
- r - rlang::flatten() 的基本等价物是什么?
- java - 为什么在这种情况下 ExecutorService 返回正确答案 19 次?错误在哪里?
- c++ - ffmpeg:sws_scale 无法将图像从 YUV 转换为 RGB
- ssl-certificate - 将证书签名请求公钥从 ECDSA 转换为 RSA
- jquery - 禁用按钮在 IE 11 中不起作用,但它在 chrome 中起作用
- android - 即使设置环境变量后也无法识别颤振
- ios - 重构我的数组迭代的建议?
- php - 自 PHP 7.2 起不推荐使用函数 read_exif_data()