修复使用TKinter从一个窗口切换到另一个窗口时的延迟问题?

2024-07-03 08:19:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个基于kiosk的应用程序开发一个使用TKinter的GUI,并且我已经开发了一个基本代码,它可以通过点击按钮从一个窗口切换到另一个窗口。你知道吗

下面是我尝试过的代码,它运行成功。然而,我发现一些延迟问题时,从一个窗口切换到另一个。当用户按下根窗口上的按钮时,在第二个屏幕显示之前有足够的延迟时间。我已经一步一步地测试了它,并意识到图像打开和显示的方式需要足够的处理时间,这会产生延迟问题。你知道吗

try:
    from Tkinter import *
    import Tkinter as tk
except ImportError:
    from tkinter import *
    import tkinter as tk

from functools import partial
from PIL import ImageTk, Image

def test(root):
    root.withdraw()
    master1 = tk.Toplevel()
    coverPhoto = Image.open('/home/pi/x.jpg')
    coverPhoto = ImageTk.PhotoImage(coverPhoto)
    lblBackground = Label(master1, width=ws, height=hs, image=coverPhoto)
    lblBackground.photo = coverPhoto
    lblBackground.pack(fill=BOTH, expand=YES)
    master1.config(width=ws, height=hs)
    master1.attributes('-fullscreen', True)
    master1.mainloop()
    return


root = tk.Tk()
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()

root.title(' Sample test ')

idPhoto = Image.open('/home/pi/x.jpg')
idPhoto = ImageTk.PhotoImage(idPhoto)
lblImg = Label(root, width=ws, height=hs, image=idPhoto)
lblImg.photo = idPhoto
lblImg.pack(fill=BOTH, expand=YES)

startImg = Image.open('/home/pi/y.jpg'  )
startImg = ImageTk.PhotoImage(startImg)
button = tk.Button(lblImg, image=startImg, highlightthickness=1, bd=0, command=lambda : test(root)) 
button.image = startImg
button.place(x=0, y=hs - 120, width=ws, height=120)
root.attributes('-fullscreen', True)
root.config(width=ws, height=hs)
root.mainloop()

有没有其他的呼叫方式可以减少处理时间?当我从一个屏幕切换到另一个屏幕时,我想要一个平稳的过程?你知道吗


Tags: fromimageimportwsrootwidthtkhs
1条回答
网友
1楼 · 发布于 2024-07-03 08:19:05

您可以在启动mainloop之前加载所有图像。你知道吗

这当然会增加应用程序的启动时间,但应该会减少窗口切换时间。你知道吗

编辑:同时将所有ImageTk.PhotoImage调用放在root = tk.Tk()之后mainloop之前。也就是说,把coverPhoto的代码从test中取出,放在rootmainloop之间。你知道吗

Edit2:窗口关闭时出现白色闪烁可能是由于raspberry pi上相对较慢的图形硬件以及X11的工作方式造成的。你知道吗

尝试使用带有两个“制表符”的^{},而不是两个顶层窗口。在相同X11窗口中隐藏和取消隐藏相关选项卡。你知道吗

相关问题 更多 >