Exception to add image in label in tkinter

     img_frame=Frame(up_frame,bd=1,bg="white")
    img_frame.place(x=1015,y=5,height=212,width=310)
    photo_frame=Frame(img_frame,bd=3,bg="green",relief=GROOVE)
    photo_frame.place(x=0,y=0,height=212,width=190)
    img=Image.open("Image/employee-icon.png")
    #img=PhotoImage(file="Image/pic.png")


    lbl_photo=Label(photo_frame,bg="white",image=img)
    lbl_photo.place(x=0,y=0,height=212,width=190)

Exception has occurred: TclError
image “<PIL.PngImagePlugin.PngImageFile image mode=P size=512x486 at 0x7F123D8888B0>” doesn’t exist
File “/home/gaurav/PythonProgram/everysolution.py”, line 176, in init
lbl_photo=Label(photo_frame,bg=“white”,image=img)
File “/home/gaurav/PythonProgram/everysolution.py”, line 524, in
obj=StudentRecord(root)
_tkinter.TclError: image “<PIL.PngImagePlugin.PngImageFile image mode=P size=512x486 at 0x7F123D8888B0>” doesn’t exist

We can’t help wihout seeing your code. Don’t forget to put code and
traceback in code fences to preserve the formatting:

 ```
 your code
 goes here
 ```

There’s a </> button in the compose toolbar to make one of thse
sections.

I believe all that is missing is that the PIL image needs to be passed through PIL.ImageTk before being passed as the image option to a Tkinter widget. Since the PIL image is created from a PNG file, PIL.ImageTk.PhotoImage() would likely be used. ActiveState has an example of this in their tutorial How To Add Images In Tkinter.

The image option for Tkinter widgets expects a string, or something which can be stringified by str(), with the name of an image already made available in Tkinter. The PIL.ImageTk functions make a PIL image available in Tkinter, as well as return an object which stringifies to the Tkinter name for the image, e.g. pyimage1. Stringifying a PIL image instead produces e.g. <PIL.PngImagePlugin.PngImageFile image mode=P size=512x486 at 0x7F123D8888B0>, which when passed directly to Tkinter will result in the error image "<PIL.PngImagePlugin.PngImageFile image mode=P size=512x486 at 0x7F123D8888B0>" doesn't exist.

1 Like

I have already installed and imported PIL