【发布时间】:2010-04-28 20:22:03
【问题描述】:
我正在用 Tkinter 写一个幻灯片程序,但我不知道如何在不绑定键的情况下转到下一个图像。
import os, sys
import Tkinter
import Image, ImageTk
import time
root = Tkinter.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
root.bind("<Escape>", lambda e: e.widget.quit())
image_path = os.path.join(os.getcwd(), 'images/')
dirlist = os.listdir(image_path)
for f in dirlist:
try:
image = Image.open(image_path+f)
tkpi = ImageTk.PhotoImage(image)
label_image = Tkinter.Label(root, image=tkpi) # ?
label_image.place(x=0,y=0,width=w,height=h)
root.mainloop(0)
except IOError:
pass
root.destroy()
我想添加一个 time.sleep(10) “而不是” root.mainloop(0) 以便它在 10 秒后转到下一个图像。现在当我按 ESC 时它会改变。我怎么能在那里有一个计时器?
编辑:我应该补充一点,即使它可以工作,我也不希望另一个线程进入睡眠状态。
【问题讨论】:
-
启动和停止主循环没有多大意义——它是一个无限循环,旨在为程序的生命周期运行。