Attempting to make a photo slide

Question:

I’m quite new to Python. I’m trying to update the image periodically. I’ve searched around but I’m still struggling to make this work how I want. I’m just going to paste the whole .py file I have.

Right now, It seems as though it’s incrementing properly. I know the __init__ function in the Window class gets run only once so it is iterating but not actually updating the ImageTk.PhotoImage object. I think it has to do with my resize_image function because in change_photo when I try to configure the label to the new image with the updated index, I just get a blank image after the allotted time.

I just think I’m not quite on the right track and need a nudge in the right direction here. Thank you

class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.pack(fill=BOTH, expand=YES)

        self.photos = getPhotos()
        self.index = 0

        self.image = Image.open(path + self.photos[self.index])

        self.img_copy = self.image.copy()

        self.imageTK = ImageTk.PhotoImage(self.image)

        self.label = Label(self, image=self.imageTK)
        self.label.pack(fill=BOTH, expand=YES)
        self.label.bind('<Configure>', self.resize_image)


    def resize_image(self, event):
        orig_width = self.image.width
        orig_height = self.image.height

        new_width = updateWidth(orig_width, orig_height)

        new_height = event.height

        self.image = self.img_copy.resize((new_width, new_height))

        self.imageTK = ImageTk.PhotoImage(self.image)
        self.label.configure(image=self.imageTK)

    def change_photo(self):
        if self.index == (len(self.photos) - 1):
            self.index = 0
        else:
            self.index += 1
            self.label.configure(image=ImageTk.PhotoImage(Image.open(path + self.photos[self.index])))
            root.after(1000, self.change_photo)


app = Window(root)
app.pack(fill=BOTH, expand=YES)

app.change_photo()

root.mainloop()


Asked By: cnelson720

||

Answers:

It looks like you are trying to update the image on the GUI. However, it seems like the image is not being updated after the initial setup in the __init__ method.

One issue could be that you are creating a new instance of ImageTk.PhotoImage every time change_photo method is called. However, it is not being referenced anywhere, so the previous instances are being garbage collected, which is why the image is not showing up on the GUI.

Try storing the ImageTk.PhotoImage instance in a class variable and updating the label with it. Like this:

class Window(Frame):
    def __init__(self, master=None):
        ...
        self.imageTK = ImageTk.PhotoImage(self.image)
        self.label = Label(self, image=self.imageTK)
        ...

    def change_photo(self):
        if self.index == (len(self.photos) - 1):
            self.index = 0
        else:
            self.index += 1
            self.img_copy = Image.open(path + self.photos[self.index])
            self.imageTK = ImageTk.PhotoImage(self.img_copy)
            self.label.configure(image=self.imageTK)
            root.after(1000, self.change_photo)

Another issue is that you are not calling resize_image in the change_photo method. You should call it after updating the label.

def change_photo(self):
    if self.index == (len(self.photos) - 1):
        self.index = 0
    else:
        self.index += 1
        self.img_copy = Image.open(path + self.photos[self.index])
        self.resize_image(self.label)
        root.after(1000, self.change_photo)
Answered By: Yosh
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.