带有python图像幻灯片的空白GUI

2024-04-25 06:56:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我在幻灯片放映期间无法显示图像。当update_image的前四行注释掉后,将显示第一个图像(1.png)。但当它没有被评论时,我得到了一个空白的图形用户界面。有人有什么建议吗?在

    import Tkinter
    import Image, ImageTk

    class App():
        def __init__(self):
            self.root = Tkinter.Tk()
            image1=Image.open('C:\Users\Jason\Desktop\ScreenShots\\1.png')
            self.root.geometry('%dx%d' % (image1.size[0],image1.size[1]))
            tkpi=ImageTk.PhotoImage(image1)
            label_image=Tkinter.Label(self.root, image=tkpi)
            label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])

            self.update_image()
            self.root.mainloop()


        def update_image(self):
            image1=Image.open('C:\Users\Jason\Desktop\ScreenShots\\2.png')
            tkpi=ImageTk.PhotoImage(image1)
            label_image=Tkinter.Label(self.root, image=tkpi)
            label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])

            print 'slide'
            self.root.after(500, self.update_image)

    app=App()

Tags: 图像imageimportselfappsizepngtkinter
1条回答
网友
1楼 · 发布于 2024-04-25 06:56:13

可能是因为您的image对象是update_image范围的本地对象,所以被垃圾回收。尝试将图像对象保存为类的属性(例如:self.tkpi

顺便说一句,如果你在幻灯片放映时一次只显示一个图像,那么就没有理由在每次创建新图像时都创建一个新标签。创建一个新图像,然后将其指定给现有的标签图像就足够了。在

我还建议您使用grid或{},而不是placeplace在一些情况下是有用的,但是通常最好使用另外两个几何管理器。在

相关问题 更多 >