Home > Net >  Can't display image dynamically in loop with tkinter
Can't display image dynamically in loop with tkinter

Time:12-03

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.

CodePudding user response:

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
...
  • Related