import tkinter as tk
from PIL import ImageTk, Image
root = tk.Tk()
def photogetter():
###global photo
photo= ImageTk.PhotoImage(Image.open("smiley.png").resize((320,240)))
label =tk.Label(root,image=photo)
canv.create_window((320,240),window=label)
canv = tk.Canvas(root,width=640,height=480)
canv.grid(row=0,column=0)
button = tk.Button(root,text="Button",command=photogetter)
button.grid(row=1,column=0)
root.mainloop()
This code does not work unless I declare the photo variable as global in my function. Can somebody explain me why do I have to declare the photo variable as global? Using local variables look more efficient to me, but it does not work.