The following code runs properly without error just does not show the image for the label inside the toplevel window
from tkinter import*
from PIL import ImageTk,Image
root = Tk()
root.title()
root.iconbitmap("SOME IMAGE PATH")
def window():
global img # HERE IS THE ONLY CHANGE
top = Toplevel()
img = ImageTk.PhotoImage(Image.open("SOME IMAGE PATH"))
label = Label(top,image=img)
label.pack()
button = Button(root,text="Open",command=window)
button.pack()
root.mainloop()
But when I run the same code just by declaring the img as global variable It shows the image and runs perfectly Given Below is that code...
from tkinter import*
from PIL import ImageTk,Image
root = Tk()
root.title()
root.iconbitmap("SOME IMAGE PATH")
def window():
global img # HERE IS THE ONLY CHANGE
top = Toplevel()
img = ImageTk.PhotoImage(Image.open("SOME IMAGE PATH"))
label = Label(top,image=img)
label.pack()
button = Button(root,text="Open",command=window)
button.pack()
root.mainloop()
CAN ANYONE TELL ME WHY THE IMAGE ONLY APPEARS WHEN WE SET IT GLOBAL ?