在Python中退出Tks主循环?

发布于 2024-08-30 20:29:27 字数 888 浏览 2 评论 0原文

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

编辑:我应该补充一点,我不希望另一个线程进行睡眠,即使它有效。

I'm writing a slideshow program with Tkinter, but I don't know how to go to the next image without binding a key.

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

I would like to add a time.sleep(10) "instead" of the root.mainloop(0) so that it would go to the next image after 10s. Now it changes when I press ESC. How can I have a timer there?

edit: I should add that I don't want another thread that does a sleep even though it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-09-06 20:29:27

你可以尝试

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

You can try

root.after(10*1000, root.quit)
够运 2024-09-06 20:29:27

无需对图像进行循环 - 您已经在循环(主循环)中运行,因此请充分利用它。执行此操作的典型方法是创建一个方法来绘制某些内容,等待一段时间,然后调用自身。这不是递归,它只是告诉主循环“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)

There's no need to do a loop over your images -- you're already running in a loop (mainloop) so take advantage of it. The typical way to do this is to create a method that draws something, waits for a period of time, then calls itself. This isn't recursion, it's just telling the main loop "after N seconds, call me again".

Here's a working example:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文