Tkinter (call from function) show picture in label only works when function variable "foto" is made GLOBAL in function ... why is this? [duplicate] Tkinter (call from function) show picture in label only works when function variable "foto" is made GLOBAL in function ... why is this? [duplicate] tkinter tkinter

Tkinter (call from function) show picture in label only works when function variable "foto" is made GLOBAL in function ... why is this? [duplicate]


This is the well known requirement of keeping a reference to an image object. It's much better to make it an attribute instead of a global:

def Show(event):    select = lbox.curselection()    selected = lbox.get(select[0])    print(selected)    image = Image.open("images/" + selected)    image = image.resize((50,50))    foto = ImageTk.PhotoImage(image)    label1 = Label(root, image=foto)    label1.grid(row=0, column=1)    label1.foto = foto # keep a reference