4

I want to insert an image into my tkinter but I received error:

TclError: image "pyimage7" doesn't exist.

I am using WinPython-64-3.3.5.9. I tried "rozmery.gif" but didn't help.

    from tkinter import *        
    from PIL import ImageTk, Image

    app_root = Tk()

    #Setting it up
    img = ImageTk.PhotoImage(Image.open("rozmery.png"))

    #Displaying it
    imglabel = Label(app_root, image=img).grid(row=1, column=1) 

    app_root.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • 3
    Please post the entire error message. – figbeam May 04 '18 at 12:08
  • there was an older post where you shall find your answers https://stackoverflow.com/questions/10133856/how-to-add-an-image-in-tkinter – stonebig May 05 '18 at 06:19
  • This sample of code works perfectly for me. Usually when I get this kind of error, this is because there are two `Tk` instances and the image does not belong to the same Tk instance as the widget I want to display it in (especially happens when I use the jupyter qtconsole). Doing `ImageTk.PhotoImage(Image.open("rozmery.png"), master=app_root )` might solve your problem. – j_4321 May 25 '18 at 09:27

2 Answers2

2

You should keep a reference to the image before placing it:

imglabel = Label(app_root, image=img)
imglabel.image = img
imglabel.grid(row=1, column=1) 
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
2

I had the same error message. I restarted the kernel (Spyder) and it worked perfectly fine. I have this kind of issue sometimes where the code works just fine but it refuses to work. Restarting the kernel is often the only way.

Chris Ze Third
  • 632
  • 2
  • 18