0
#Import python tkinter and Image library
from tkinter import *
from PIL import ImageTk, Image
#making function
def func():
    top = Tk()
    image = ImageTk.PhotoImage(file = 'my_image.png')
    b1 = Button(top, image=image).grid(row=0, column=0)
    top.mainloop()
#main
win = Tk()
#I want this button to open another window where it shows a button with an Image on it. 
b2 = Button(top, text='Click to open new window', command=func).grid(row=0, column=0)
win.mainloop()

#Output
#pyimage1 doesn't exist.

It should have shown a window with a button that takes me to another window where there is an image button. This should have worked but in mine, it shows the above output.

  • 3
    Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – Matiiss May 13 '21 at 12:29
  • 1
    Actually, what @Matiiss linked to is certainly part of the problem, but multiple calls to `Tk()` are also an issue here - use `Toplevel()` to create additional windows. – jasonharper May 13 '21 at 12:32

0 Answers0