繁体   English   中英

延迟打开Tkinter多个Windows(Python)

[英]Open Tkinter multiple Windows with delay (Python)

我一直在尝试打开几个Tkinter窗口,每个Tkinter窗口之间要延迟几秒钟。

到目前为止,我的脚本:

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()

我也尝试使用sleep()和after()函数,但没有成功。 它在准备窗口之前等待时间,但是在我设置时间之后同时将它们全部显示

after需要没有()和参数的函数名

运用

root.after(1000, showPic(i+1))

就好像

result = showPic(i+1)
root.after(1000, result)

您可以使用lambda

root.after(1000, lambda:showPic(i+1))

因为它几乎就像

def new_function():
    showPic(i+1)

root.after(1000, new_function)

要么

root.after(1000, showPic, i+1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM