0

i'm trying to make a photo slideshow program. it isn't working as i want to go to the next photo in the list using the self.counter variable, but the value of self.counter is going back to 0 as it forgets the fact that i changed the value. i didn't think i could use configure as then the buttons to go to the next image wouldn't work either.

i hope you can help :) much appreciated.

from tkinter import *

class SlideShowGUI:

    def __init__(self, parent, counter = 0):
        
        self.counter = counter
        
        self.images_list = ["smiley.png", "carrot.png", "bunny.jpg"]
        self.photo = PhotoImage(file = self.images_list[self.counter])

        self.b1 = Button(parent, text = "", image = self.photo, bg = "white")
        self.b1.grid(row = 0, column = 0)
        
        back = Button(parent, width = 2, anchor = W, text = "<", relief = RIDGE, command = self.previous_image)
        back.grid(row = 1, column = 0)
        
        forward = Button(parent, width = 2, anchor = E, text = ">", relief = RIDGE, command = self.next_image)
        forward.grid(row = 1, column = 1)

    def previous_image(self):
        self.counter -= 1
        self.photo.configure(file = self.images_list[self.counter])        

    def next_image(self):
        self.counter += 1
        self.photo.configure(file = self.images_list[self.counter])  

#main routine

if __name__ == "__main__":
    root = Tk()
    root.title("hello")
    slides = SlideShowGUI(root)
    root.mainloop()

sorry it will not work without getting images !

enter image description here

error message if i click next button two times

coder1234
  • 51
  • 1
  • 1
  • 10
  • It didn't forget anything, You initiated a new class and that class, has the same self.counter as the current one, so You could try adding an argument to the class named counter and set the default to say 0 and when createing the next class set it to the next one – Matiiss Apr 11 '21 at 06:41
  • About that erro: https://stackoverflow.com/questions/47357090/tkinter-error-couldnt-recognize-data-in-image-file does this answer the question? – Matiiss Apr 11 '21 at 07:06

1 Answers1

1

use this instead:

def previous_image(self):
        self.counter -= 1
        self.photo.configure(file = images_list[self.counter])        

    def next_image(self):
        self.counter += 1
        self.photo.configure(file = images_list[self.counter])

except You have to watch out for List Out Of Index errors

Also why are You using global images_list? There seems to be no point. If You wanted to reuse that list in the class You could have just named it self.images_list = [your, items].

The error You are getting: read this

Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • this works! thank you, however, it doesn't work for the third image, as it thinks it is the 3rd item in the list, when it is the 2nd. – coder1234 Apr 11 '21 at 06:55
  • which part doesn't work for the third image? first one? since that actually would make sense since You would have multiple classes that try to access an image so try the second approacd – Matiiss Apr 11 '21 at 06:58
  • I will add the new code I have changed and an image of the error message – coder1234 Apr 11 '21 at 07:01
  • @coder1234 also why are You using button as image holder? You can use label instead – Matiiss Apr 11 '21 at 07:01
  • oh yes i just changed the button to label ty – coder1234 Apr 11 '21 at 07:03
  • @coder1234 that error has nothing to do with the fact that You are not using classes (or at least it wouldn't make sense). Anyhow You shouldn't be creating classes like You were in the original code because each class has their own space in memory so they take it up and if there were a lot of images the app could get slow. So the second approach in my answer is way better, I already linked to why You may be getting that error but it shouldn't be related to my solution – Matiiss Apr 11 '21 at 07:08
  • it says there is no module PIL – coder1234 Apr 11 '21 at 07:19
  • I changed the image for the bunny to one with the same tag .png and it worked! thank you so much for all your help. very kind !! – coder1234 Apr 11 '21 at 08:06