Python insert image into Tkinter error

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()

Upvotes: 4

Views: 5441

Answers (2)

Chris Ze Third
Chris Ze Third

Reputation: 359

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.

Upvotes: 4

Billal BEGUERADJ
Billal BEGUERADJ

Reputation: 22804

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) 

Upvotes: 1

Related Questions