0

I fetch all the names of photos from the database. How to display all photos on the label?

Here is my code:

photo displaying.
       for row in rows:
            photo=row[2]
            img1="images/"+str(photo)+".png"
            img= PhotoImage(file=img1)
            photol= Label(photoframe,image=img,width=150,height=100)
            photol.pack()

            
            
            
            


                            
            
10 Rep
  • 2,217
  • 7
  • 19
  • 33
Md shahin
  • 11
  • 1
  • 1
    Welcome to Stack Overflow! Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without a **clear problem statement** are not useful to other readers. See: [How to create a Minimal, Reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – rizerphe Jun 21 '20 at 19:56
  • 2
    Please format your code properly, [click here to learn how](https://stackoverflow.com/help/formatting). – rizerphe Jun 21 '20 at 19:56
  • Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – 10 Rep Jun 22 '20 at 05:47

1 Answers1

1

You re-use the name img for every new image. When the loop exits all references to images except for the last one will be lost.

You can save a reference to the image in a label in the label object:

img = PhotoImage(file=img1)
photol = Label(photoframe, image=img, width=150, height=100)
photol.image = img   # Save reference to image within label
etc...
figbeam
  • 7,001
  • 2
  • 12
  • 18