我希望我的tkinter图像每4秒出现一次,而不是一次出现



嘿,我想要的是同一张图像在不同的位置弹出4秒钟。就在这时,所有的照片都突然出现了。我试着在for循环中添加time.sleep,但所有的照片都会以任何方式同时弹出。请试用我的代码,并向我展示如何修复,因为我对python非常陌生。感谢

from tkinter import *
from PIL import ImageTk, Image
from os import walk, getenv, system
from shutil import copyfile
import subprocess
import requests, subprocess, os, tempfile
import os
import tkinter.messagebox
import time

# This is the fuction that makes the original photo and is what root.after calls to later on in the code. 
def ShowAnotherWin(i):
win = Toplevel()
image = ImageTk.PhotoImage(Image.open('C:/Users/capture.PNG'))
win.geometry(i)
canvas =Canvas(win, width=420, height=560)
canvas.create_image(0, 0, image= image, anchor=NW)
canvas.pack()
win.overrideredirect(1)
win.mainloop() 


# This is the list it, it consists of different y and x interpects point and will be combined with the showanotherwin to makee a unique image pop up.
YourImageList = ['420x544+0+0',  
time.sleep(1)
'420x544+200+600',
time.sleep(88)
'420x544+1300+100',
'420x544+1500+800',
'420x544+900+500',
'420x544+500+75',

]
# this code right here takes all the elements in the list and combines it with the showanotherwin. It then displays on screen.
root = Tk()
for i in YourImageList:
root.after(0, lambda i=i: ShowAnotherWin(i))
# time.sleep(1)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> leads to all the photos poping up with the only, effect being the duration of the photos poping up. Please help!!
# this right here is just making another photo by itself dont worry about it.   
image = ImageTk.PhotoImage(Image.open('C:/Users/capture.PNG'))
root.geometry('420x544+750+200')
canvas = Canvas(root, width=420, height=560)
canvas.create_image(0, 0, image= image, anchor=NW)
canvas.pack()
root.overrideredirect(1)
root.mainloop()

简单的方法是:

for i, x in enumerate(YourImageList):
root.after(i*4000, lambda x=x: ShowAnotherWin(x))

另一种方法是在ShowAnotherWin():中调用after()

def ShowAnotherWin(i=0):
if i < len(YourImageList):
win = Toplevel()
image = ImageTk.PhotoImage(file='C:/Users/capture.PNG')
win.geometry(YourImageList[i])
canvas =Canvas(win, width=420, height=560)
canvas.create_image(0, 0, image= image, anchor=NW)
canvas.pack()
canvas.image = image # need to keep a reference to image
win.overrideredirect(1)
#win.mainloop()  # no need to call mainloop() here
root.after(4000, ShowAnotherWin, i+1)
...
ShowAnotherWin()

您基本上会将它们全部武装起来,以便在0毫秒延迟后显示在此处root.after( 0 ,...

for i in YourImageList:
root.after(0, lambda i=i: ShowAnotherWin(i))

相关内容

最新更新