I am trying to display photo.png
in two different ways: first inside the mainloop
and then inside a function called show_photo_in_another_window()
. The code is identical in both. The image displays correctly inside mainloop
but the function gives a blank window. Why? How to fix it? Thanks in advance. I am using Python 3.8.
import tkinter as tk
from PIL import Image, ImageTk
def show_photo_in_another_window():
win2 = tk.Toplevel()
win2.title('Second window')
photo = ImageTk.PhotoImage(Image.open("photo.png"))
tk.Label(win2, image=photo).pack()
root = tk.Tk()
win = tk.Toplevel()
win.title('First window')
photo = ImageTk.PhotoImage(Image.open("photo.png"))
tk.Label(win, image=photo).pack()
show_photo_in_another_window()
root.mainloop()