everyone, I want to use picture inside toplevel. But if I don't use global statement, I can't see my picture in toplevel which is the reason why I really confuse about. Does anyone know the reasons?
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
def open():
top = Toplevel()
my_image = ImageTk.PhotoImage(Image.open(
"C:\\Users\\86177\\Desktop\\python_source\\homo.jpeg"
))
Label(top, image = my_image).pack()
Button(root, text = "open second window", command = open).pack()
root.mainloop()
In this code, when I click button to enter into toplevel, I can't see any image and picture in there. But when I add global statement inside function, I can see image inside toplevel.
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
def open():
global my_image
top = Toplevel()
my_image = ImageTk.PhotoImage(Image.open(
"C:\\Users\\86177\\Desktop\\python_source\\homo.jpeg"
))
Label(top, image = my_image).pack()
Button(root, text = "open second window", command = open).pack()
root.mainloop()
The location of file is right. In this code, I can see my image. Actually, I know global statement, but I don't think it is necessary to add global statement here because hole toplevel code is inside the open() function. Can someone explains the reason? Thank you so much.