[英]Open Tkinter multiple Windows with delay (Python)
I've been trying to open several Tkinter windows with a delay of couple seconds between each Tkinter window. 我一直在尝试打开几个Tkinter窗口,每个Tkinter窗口之间要延迟几秒钟。
My Script so far : 到目前为止,我的脚本:
import Tkinter as tk
import os
from PIL import Image, ImageTk
from win32api import GetSystemMetrics
from time import sleep
from random import uniform
root = tk.Tk()
tk.Label(root, text="this is the root window").pack()
root.geometry("10x10")
l = []
def showPic(i):
if(i<5):
loc = os.getcwd() + '\pictures\pic%s.jpg' % i
img = Image.open(loc)
img.load()
photoImg = ImageTk.PhotoImage(img)
l.append(photoImg)
window = tk.Toplevel()
window.geometry("750x750+%d+%d" % (uniform(0, GetSystemMetrics(0) - 750), uniform(0, GetSystemMetrics(1) - 750)))
tk.Label(window, text="this is window %s" % i).pack()
tk.Label(window, image=photoImg).pack()
root.after(1000, showPic(i+1))
else:
return
root.after(0, showPic(1))
root.mainloop()
I also tried using the sleep() and after() functions with no success. 我也尝试使用sleep()和after()函数,但没有成功。 It waits the time before preparing the window, but shows them all together, simultaniously, after the time I set 它在准备窗口之前等待时间,但是在我设置时间之后同时将它们全部显示
after
expects function name without ()
and arguments after
需要没有()
和参数的函数名
Using 运用
root.after(1000, showPic(i+1))
is like 就好像
result = showPic(i+1)
root.after(1000, result)
You can use lambda
您可以使用lambda
root.after(1000, lambda:showPic(i+1))
because it is almost like 因为它几乎就像
def new_function():
showPic(i+1)
root.after(1000, new_function)
or 要么
root.after(1000, showPic, i+1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.