i created a listbox with filenames from a directory. Selecting a filename trickers the show function which will display the foto
This does only works when i make the foto variable global in show() Can anybody explain to me why it only works when the foto variable is global (Without assigning global gives no error, but does not show the picture ) Does not seem logic to me i only use foto variable in the show function. Thanks
from tkinter import *
from PIL import ImageTk,Image
from os import listdir
from os.path import isfile, join
def Show(event):
global foto
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)
root=Tk()
root.geometry("")
mypath = "/home/cor/pyprojects/images"
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
onlyfiles.sort()
lbox = Listbox(root)
lbox.insert("end", *onlyfiles)
lbox.bind("<<ListboxSelect>>", Show)
lbox.grid(row=0, column=0)
root.mainloop()