在Python中退出Tks mainloop?

时间:2010-04-28 20:22:04

标签: python tkinter tk

我正在使用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时它会改变。我怎么能在那里有一个计时器?

编辑:我应该补充一点,即使它有效,我也不想要另一个睡眠的线程。

2 个答案:

答案 0 :(得分:5)

你可以尝试

root.after(10*1000, root.quit)

答案 1 :(得分:5)

没有必要对图像进行循环 - 您已经在循环(mainloop)中运行,因此可以利用它。执行此操作的典型方法是创建一个绘制内容,等待一段时间然后调用自身的方法。这不是递归,它只是告诉主循环“在N秒后,再次给我打电话”。

这是一个有效的例子:

import glob
import Tkinter

class Slideshow:
    def __init__(self, pattern="*.gif", delay=10000):

        root = Tkinter.Tk()
        root.geometry("200x200")

        # this label will be used to display the image. Make
        # it automatically fill the whole window
        label = Tkinter.Label(root) 
        label.pack(side="top", fill="both", expand=True)

        self.current_image = None
        self.image_label = label
        self.root = root
        self.image_files = glob.glob(pattern)
        self.delay = delay # milliseconds

        # schedule the first image to appear as soon after the 
        # the loop starts as possible.
        root.after(1, self.showImage)
        root.mainloop()


    def showImage(self):
        # display the next file
        file = self.image_files.pop(0)
        self.current_image = Tkinter.PhotoImage(file=file)
        self.image_label.configure(image=self.current_image)

        # either reschedule to display the file, 
        # or quit if there are no more files to display
        if len(self.image_files) > 0:
            self.root.after(self.delay, self.showImage)
        else:
            self.root.after(self.delay, self.root.quit)

    def quit(self):
        self.root.quit()


if __name__ == "__main__":
    app=Slideshow("images/*.gif", 1000)