user1217458
user1217458

Reputation: 21

blank GUI with python image slideshow

I'm having problems getting images to display during a slideshow. When the first four lines of update_image is commented out, the first image (1.png) is shown. But when its not commented, I get a blank GUI. Anyone have any suggestions?

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

Upvotes: 0

Views: 1603

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386020

Probably what is happening is that your image object is getting garbage collected since it is local to the scope of update_image. Try saving the image object as a property of the class (eg: self.tkpi)

By the way -- if you're doing a slide show where you are showing a single image at a time, there's no reason to create a new label each time you create a new image. It's sufficient to create a new image and then assign it to the existing label image.

I also recommend you use grid or pack rather than place. place is useful in a few circumstances, but it's generally better to use the other two geometry managers.

Upvotes: 1

Related Questions