0

I want python to iterate images in a directory and pack that to the GUI

from tkinter import *
from pathlib import Path
from PIL import ImageTk, Image

root = Tk()

images = []

paths = Path("images").glob('**/*.png')
for path in paths:
    photo = Image.open(path)
    photo.thumbnail((100, 100))
    img = ImageTk.PhotoImage(photo)
    label = Label(image=img)
    images.append(label)

for image in images:
    image.pack()

root.mainloop()

But the output is just the last picture on the directory:

enter image description here

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • Keep a list of all of the `img` objects. – TheLizzard Aug 19 '21 at 11:08
  • 4
    This question is essentially the same problem as this: [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) – TheLizzard Aug 19 '21 at 11:09

1 Answers1

0

here my attempt:


from tkinter import *
from pathlib import Path
from PIL import ImageTk, Image

pippo = Tk()

images = []

paths = Path("images").glob('**/*.png')
for path in paths:
    print(path)
    photo = Image.open(path)
    photo.thumbnail((100, 100))
    img = ImageTk.PhotoImage(photo)
    images.append(img)

print(images)

for img in images:
    label = Label(pippo, image=img)
    label.pack(side = 'bottom')


pippo.mainloop()

output starting from three colored circles is:

enter image description here

pippo1980
  • 2,181
  • 3
  • 14
  • 30