tkinter canvas tag stuff

James Stroud jstroud at ucla.edu
Fri Dec 16 15:45:47 EST 2005


Tuvas wrote:
> I'm trying to display a picture on a Tkinter Canvas. It seems to work
> fine the first time that it is displayed. However, subsequent times
> running shows an error like this:
> 
> TCLerror: Wrong # args: should be ".-1211472948 .-1211470996 addtag tag
> searchCommand ?arg arg ...?
> 
> My code works like this:
> 
> if (pic):
>      canvas.delete(pic)
> im=Image.open(path)
> image=ImageTk.PhotoImage(im)
> pic=canvas.create_image(1,1,anchor="nw", image=image)
> canvas.addtag(pic)
> 
> 
> Note that path is a path selected outside of the function. What happens
> is the image is displayed, but with the error message. I think I should
> either have to put in some kind of argument into the addtag, which
> isn't documented well, or use a similar but different function. Thanks
> for the help!
> 

There are at least 2 better (more common, and thus closer the the "one-- 
and preferably only one") ways to do this.

1. include a tag option when creating the image
   pic=canvas.create_image(1,1,anchor="nw", image=imagem, tag="some_tag")
2. use the Canvas.addtag_withtag method:
   canvas.add_tag_withtag('some_tag', pic)

These examples assume the tag you want to use is called "some_tag".

A brief description on the use of tags is at

http://www.pythonware.com/library/tkinter/introduction/x2017-concepts.htm#AEN2057

BTW, the error message you are getting is an artefact of Tk. I have yet 
to find any Tkinter-specific documentation on the Canvas.addtag method, 
but the Tk documentation suggests the following is equivalent to the above:

   canvas.addtag("some_tag", "withtag", pic)

James



More information about the Python-list mailing list