-2

I'm making a tkinter application in python, in which I have a frame where I want to show 4-5 pictures. The code above the hashtags is working fine, it shows 1 pictures.

But the code below the picture is bad for some reason. It doesn't show anything, but it should show 4 pictures. Do I need to make a list from the PhotoImages or is something wrong with the self-s?

Thanks

    self.photo = PhotoImage(file=onlyfiles[0])
    self.photo = self.photo.subsample(4, 4)
    label=Label(self, image=self.photo)
    label.pack()
    ################################################
    image_labels = []
    for i in range(len(onlyfiles)): # 4
        self.gombi = PhotoImage(file=onlyfiles[i])
        self.gombi = self.gombi.subsample(4, 4)
        image_labels.append(Label(self, image=self.gombi))
        image_labels[i].pack()
Marci
  • 427
  • 2
  • 9
  • 20
  • Possible duplicate of [How to add an Image to a widget? Why is the image not displayed?](https://stackoverflow.com/questions/37515847/how-to-add-an-image-to-a-widget-why-is-the-image-not-displayed) – Nae Dec 05 '17 at 00:11
  • didn't you ask this question before ? And you get link to Note about garbage collector. – furas Dec 05 '17 at 00:12
  • 1
    Also see [this](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function/16424553#16424553). – Nae Dec 05 '17 at 00:13

1 Answers1

2

Python does automatic garbage collection, so your images are probably getting gobbled up, even though you think they should still be there. Try storing references to your self.gombi values in another array, as well. That will most likely do the trick.

Try something like this:

self.photo = PhotoImage(file=onlyfiles[0])
self.photo = self.photo.subsample(4, 4)
label=Label(self, image=self.photo)
label.pack()
################################################
self.image_labels = []
self.image_gombis = []
for i in range(len(onlyfiles)): # 4
    gombi = PhotoImage(file=onlyfiles[i])
    gombi = gombi.subsample(4, 4)
    self.image_gombis.append(gombi)
    self.image_labels.append(Label(self, image=gombi))
    self.image_labels[i].pack()

I haven't tried to run this, but it should put you on the right path...

GaryMBloom
  • 5,350
  • 1
  • 24
  • 32
  • Could you please tell me what I should add to my code below the hastags to get it working? Thanks – Marci Dec 04 '17 at 23:58