Python tkinter:在按钮中实现幻灯片放映功能?

Python tkinter:在按钮中实现幻灯片放映功能?,python,tkinter,slideshow,Python,Tkinter,Slideshow,我现在正在制作一个tkinter gui程序 我想在单击def GUI\u PART,但在代码中,幻灯片放映无效 请帮忙 class mainapp(): def slide(self): root1=Tk() self.root1.geometry("+{}+{}".format(70, 100)) title("a simple Tkinter slide show") # delay in seconds (

我现在正在制作一个tkinter gui程序 我想在单击
def GUI\u PART
,但在代码中,幻灯片放映无效

请帮忙

class mainapp(): 
    def slide(self):
         root1=Tk()
         self.root1.geometry("+{}+{}".format(70, 100))
         title("a simple Tkinter slide show")
         # delay in seconds (time each slide shows)
         delay = 2.5
         imageFiles=glob.glob('/home/imagefolder/*.png')
         photos = [PhotoImage(file=fname) for fname in imageFiles]
         button = Button(root1,command=root1.destroy)
         button.pack(padx=5, pady=5)
         for photo in photos:
             button["image"] = photo
             root1.update()
             time.sleep(delay)

  def GUI_PART(self, Master):
        self.master = Master
        Master.title("Start")
        self.masterFrame = Frame(self.master)
        self.masterFrame.pack() 
        ...        
        self.boldbutton = Button(self.tool3_frame, text="Slide show",command=self.slide)
        self.boldbutton.pack(side=LEFT) 
GUI工具包是事件驱动的,Tkinter也不例外

这意味着程序在
mainloop()
中运行,并且不能使用带有睡眠的循环来显示图像

您应该做的是在应用程序对象中保存图像路径列表,并使用
after()
方法。此外,我将使应用程序类从Tk继承

示例(Python2中的Python3使用
Tkinter
而不是
Tkinter
):


有人帮你解决这个问题吗?你不想在tkinter程序中多次调用
Tk()。改为创建一个窗口。还要确保在创建
mainapp
实例后调用
GUI\u PART()
方法。
import glob
import tkinter as tk
from PIL import Image, ImageTk


class ImageViewer(tk.Tk):

    def __init__(self):
        """Create the ImageViewer."""
        # You can press q to quit the program.
        self.bind_all('q', self.do_exit)
        # Attributes for the image handling.
        self.image_names=glob.glob('/home/imagefolder/*.png')
        self.index = 0
        self.photo = None
        # We'll use a Label to display the images.
        self.label = tk.Label(self)
        self.label.pack(padx=5, pady=5)
        # Delay should be in ms.
        self.delay = 1000*2.5
        # Display the first image.
        self.show_image()

    def show_image(self):
        """Display an image."""
        # We need to use PIL.Image to open png files, since
        # tkinter's PhotoImage only reads gif and pgm/ppm files.
        image = Image.open(self.image_names[index])
        # We need to keep a reference to the image!
        self.photo = ImageTk.PhotoImage(image)
        self.index += 1
        if self.index == len(self.image_names):
            self.index = 0
        # Set the image
        self.label['image'] = self.photo
        # Tell tkinter we want this method to be called again after a delay.
        self.after(self.delay, self.show_image)

    def do_exit(self, event):
        """
        Callback to handle quitting.

        This is necessary since the quit method does not take arguments.
        """
        self.quit()

root = ImageViewer()
root.mainloop()