0

This code of program is not running and showing error image "pyimage2" doesn't exist

import tkinter 
from PIL import ImageTk, Image

def imagine():
     window1 = tkinter.Tk()
     window1.geometry("1000x300")
     img = ImageTk.PhotoImage(Image.open("1.jpg"))
     label1= tkinter.Label(window1, image = img)
     label1.place(x=10,y=2)

window = tkinter.Tk()
window.geometry("1000x300")
img = ImageTk.PhotoImage(Image.open("1.jpg"))
label1= tkinter.Label(window, image = img)
label1.place(x=10,y=2)
b1 = tkinter.Button(window,text="E",width=50,command=imagine ).place(x=100,y=2)

i want image on window1 also

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

1

In the "imagine" you are trying to create a new instance of Tk() which already exists. (we can only have a single instance of Tk) For an additional window, create instance of Toplevel().

I tested your code with fix and new window open without any error, but the image still doesn't appear.

To fix this, I passed img as a parameter using the lambda function.

import tkinter 
from PIL import ImageTk, Image

def imagine(_img):
    
    window1 = tkinter.Toplevel() #creating new TopLevel window
    window1.geometry("1000x300")
    label1= tkinter.Label(window1, image= _img)
    label1.place(x=10,y=2)

window = tkinter.Tk()
window.geometry("1000x300")
img = ImageTk.PhotoImage(Image.open("1.jpg"))
#img2 = ImageTk.PhotoImage(Image.open("other image.jpg"))
label1= tkinter.Label(window, image = img)
label1.place(x=10,y=2)
b1 = tkinter.Button(window,text="E",width=50, command=lambda: imagine(img)).place(x=100,y=2)


window.mainloop()