在Python中如何退出Tk主循环?

1 投票
2 回答
9224 浏览
提问于 2025-04-15 22:08

我正在用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 个回答

5

其实你不需要对你的图片进行循环处理,因为你已经在一个循环里了(主循环)。所以可以利用这个主循环。通常的做法是创建一个方法,这个方法先画出一些东西,然后等一段时间,再自己调用自己。这里并不是递归,只是告诉主循环“过了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)
5

你可以试试这个

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

撰写回答