tkinter canvas tag stuff

T

Tuvas

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!
 
J

James Stroud

Tuvas said:
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
 
J

James Stroud

Tuvas said:
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!

I think I killed my last post trying to fix a typo. I'm very new to
using NNTP. Forgive me if this is a close duplicate to something you
already have in your inbox.

Anyway, my corrected message goes like something like this:

There are two better (more common) ways to do this:

1. Use the tag keyword argument when creating the canvas_image:
pic=canvas.create_image(1,1,anchor="nw", image=image, tag="some_tag")
2. Use the Canvas.addtag_withtag method:
canvas.addtag_withtag('some_tag', pic)

I have yet to find Tkinter-specific documentation on the Canvas.addtag
method, but the Tk documentation suggests this has the same affect as above:

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

James
 
T

Tuvas

It's funny, I can put in more variables than needed, it doesn't even
call the function, and yet, magically, the picture appears. Odd... Just
wish I could solve the problem...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top