[Tkinter-discuss] Canvas tag bindings not going away?

Michael Lange klappnase at web.de
Tue Sep 1 20:57:51 CEST 2009

Hi Bob,

On Tue, 1 Sep 2009 12:07:45 -0600
Bob Greschke <bob at passcal.nmt.edu> wrote:

> Doesn't ACanvas.delete(ALL) get rid of bindings too?  I'm drawing  
> regular map dots (create_oval) on a Canvas "map" and each one gets a  
> Tag, like "P1", "P2", etc.  I do a tag_bind to <Button-3> to get a  
> popup menu item for each dot.  Everything works fine.
> I then have two radiobuttons, one for a regular map (described
> above), and one for drawing the dots a different color depending on  
> elevation.  When the Redraw button is hit with the elevation map
> mode selected the canvas gets cleared, the background color gets
> changed, a .delete(ALL) is done (and a .find_all() returns ()), I
> redraw the create_oval()s with the same kind of Tags ("P1", "P2",
> etc.), but without doing a tag_bind to <Button-3> (same bunch of
> code, just with an if-statement to control which kind of dot gets
> drawn).  The new dots still react to a <Button-3> event like as if
> the TAG is still bound to the <Button-3> event.  If I start all over,
> but draw the elevation map first then the dots don't do the popup
> menu.  If I then redraw with the normal map option the popup menu
> bind gets bound to the dots from that point on.
> Do I have to loop through all of the items and delete the tags
> before doing the .delete(ALL) or something?  This could be major.  I
> use a lot of Tags and binds on a lot of Canvases, but I've never
> noticed this before.  It's doing this on several OSs and versions of  
> everything, so it must be me.
> Thanks!
> Bob

If I understand you correctly, what you describe is what I think that
tag bindings are supposed to do. I tried the following:

>>> from Tkinter import *
>>> c = Canvas()
>>> c.pack()
>>> def test(ev):
...     print 'foo'
>>> c.tag_bind('tag_foo', '<1>', test)
>>> c.create_rectangle(0, 0, 100, 100, fill='blue', tags=('tag_foo'))
>>> foo

The "foo"s at the end of the snippet are of course from clicking the
blue rectangle. The point here is, that the tag-binding is obviously
active without any items being created, and I believe that is exactly
what it is for; if you want the binding to be removed when the item is
deleted, you should bind to the item-id instead, or maybe use
tag_unbind() to remove the bindings.

I hope this helps


More information about the Tkinter-discuss mailing list