Why in the world of Tkinter breaks using canvas.create_image?

I have a Python GUI application that I am going to use on both Windows and Mac. Tkinter documentation is not the best and google fu let me down.

In short, I'm doing

c = Canvas(
    master=frame,
    width=settings.WINDOW_SIZE[0],
    height=settings.WINDOW_SIZE[1],
    background=settings.CANVAS_COLOUR
)
file = PhotoImage(file=os.path.join('path', 'to', 'gif'))
c.create_bitmap(position, image=file)
c.pack()
root.mainloop()

      

If I comment out the create_bitmap line, the application renders fine. If I comment it back, I get the following error:

_tkinter.TclError: unknown option "-image"

Which is strange. Tkinter is fine as per Python tests (i.e. Imports _tkinter, Tkinter and executes Tk()

). I have since installed PIL against my Windows installation (XP SP3, Python 2.6), imagining it doing some heavy lifting at a low level. This does not seem to be the case; I am still getting the above error.

Full stack trace, excluding the already inserted code:

File "C:\Python26\lib\lib-tk\Tkinter.py", line 2153, in create_bitmap
return self._create('bitmap', args, kw)
File "C:\Python26\lib\lib-tk\Tkinter.py", line 2147, in _create
*(args + self._options(cnf, kw))))

      

Anyone able to shed some light?

+1


source to share


3 answers


Tk has two types of graphics, bitmap and image. Images come in two flavors, bitmaps and photographs. Bitmaps and bitmaps are not the same thing, which leads to confusion in documents. PhotoImage creates an image like a photo and needs an image object in the canvas, so the solution, as you already did, is using create_image.



+4


source


Short answer: Don't use create_bitmap when you want to use create_image.



+2


source


The method create_bitmap()

has no argument image

; instead it has bitmap

.

The error you are getting is because in Tkinter the Tcl interpreter is running embedded in the Python process, and all GUI interactions go between Python and Tcl; so the error you are getting is because Tcl says "I don't know of any -image option in the .create_bitmap call".

Anyway, as Jeff said, you probably need a method create_image

.

0


source







All Articles