0

I'm trying to display image in loop with tkinter module but only the last image is display. This is the function code :

if (os.path.isdir(album_metadata_repository)==True):
    global iteration
    iteration = 0
    print("Music repository exist")
    for path, subdirs, files in os.walk(album_metadata_repository):
        for name in files:
            #while (iteration < 8):
            image=""
            if name.endswith(".txt"):
                album_data = os.path.join(path, name)
                set_shape(album_data, iteration)
                img = ImageTk.PhotoImage(Image.open(cover_path))
                fenetre.image=img
                print(cover_path)
                Frame1 = Frame(fenetre, borderwidth=2, relief=FLAT)
                Frame1.grid(row=0,column=iteration)
                Label(Frame1, image=img).pack(padx=20, pady=10)
                Label(Frame1, text=artist, font=f).pack(padx=20, pady=0)
                Label(Frame1, text=album).pack(padx=20, pady=0)
                Label(Frame1, text=year).pack(padx=20, pady=0)
                Label(Frame1, text=genre).pack(padx=20, pady=0)
                Button1 = Button(fenetre, text=iteration, borderwidth=1, cursor="circle", command=lambda: display_read_metadata(album_data))
                Button1.grid(row=1, column=iteration)
                Label(Button1).pack(padx=20, pady=0)
                iteration+=1
                #panel.img=img

In the function everything works except the image display just the last one display. Thanks in advance for your help.

Aldeoxis
  • 3
  • 1
  • It is because you used `fenetre.image` to store the reference of images, so only the last image is referenced and those previous images will be garbage collected due to no variable referencing them. – acw1668 Dec 02 '22 at 09:01
  • 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) – Thingamabobs Dec 02 '22 at 09:12

1 Answers1

0

It is because you used fenetre.image to store the reference of images, so only the last image is referenced and those previous images will be garbage collected due to no variable referencing them.

It is better to use an attribute of the label holding the image to store the reference of the image instead:

...
            if name.endswith(".txt"):
                album_data = os.path.join(path, name)
                set_shape(album_data, iteration)
                img = ImageTk.PhotoImage(Image.open(cover_path))
                #fenetre.image=img
                print(cover_path)
                Frame1 = Frame(fenetre, borderwidth=2, relief=FLAT)
                Frame1.grid(row=0,column=iteration)
                lbl = Label(Frame1, image=img)
                lbl.pack(padx=20, pady=10)
                lbl.image = img # save the image reference
                Label(Frame1, text=artist, font=f).pack(padx=20, pady=0)
                Label(Frame1, text=album).pack(padx=20, pady=0)
                Label(Frame1, text=year).pack(padx=20, pady=0)
                Label(Frame1, text=genre).pack(padx=20, pady=0)
                Button1 = Button(fenetre, text=iteration, borderwidth=1, cursor="circle", command=lambda: display_read_metadata(album_data))
                Button1.grid(row=1, column=iteration)
                Label(Button1).pack(padx=20, pady=0)
                iteration+=1
...
acw1668
  • 40,144
  • 5
  • 22
  • 34