How to embed an image in a text widget

I know that you can embed an image in a Tkinter text widget, but I could not find a simple sample code. In particular, I need to embed jpg, so according to the docs, it seems to me that I need to use the photoimage class

I tried using this:

img=PhotoImage ( file=imgfn ) text.image_create(image=img) 

where imgfn is the name of the image file and the text is my text widget, but I get "_tkinter.TclError: the data in the image file could not be recognized ..."

Thanks for any help!

+4
source share
1 answer

PhotoImage processes only GIF and PGM/PPM files. To use JPEG with Tkinter, you can use the Python Imaging Library (PIL) to create PhotoImage .

 from PIL import Image, ImageTk img = Image.open("yourimg.jpg") photoImg = ImageTk.PhotoImage(img) 

Alternatively, you can use only one of the other supported formats for PhotoImage , if possible.

+5
source

All Articles