Tk Canvas objects - Deletable?

Randall Hopper aa8vb at vislab.epa.gov
Wed May 5 06:52:05 EDT 1999


How does one delete Tkinter Canvas objects?  Or do they ever get deleted?

The attached Tk script illustrates my problem.  group doesn't ever get
deleted for some reason.

What's a good way to debug this?  Is there any way to inspect the Python
reference counts for an object?

Thanks,

Randall
-------------- next part --------------
#!/usr/bin/env python

from Tkinter import *
from Canvas import *

class PlaceableGroup( Group ):

  def __init__( self, canvas, tag = None ):
    Group.__init__( self, canvas, tag )
    self.canvas = canvas
    Widget.bind( self.canvas, "<Button-1>", self.__MouseDownCB )

  def __del__( self ):
    print "del method invoked"
    self.unbind()
    self.delete()

  def unbind( self ):
    Widget.unbind( self.canvas, "<Button-1>" )

  def __MouseDownCB( self, event ):
    rect = Rectangle( self.canvas, event.x-5, event.y-5,
                                   event.x+5, event.y+5 )
    self.addtag_withtag( rect )

root = Tk()
canvas = Canvas( root )
canvas.pack()
btn = Button( text="Click canvas, and hit me to destroy the group",
              command=root.quit )
btn.pack()
group = PlaceableGroup( canvas )
root.mainloop()

#
# We would like to kill "group" here, but no such luck
#
group = None
btn.configure( text="You'd think the group wasn't bound anymore, but..." )
root.mainloop()


More information about the Python-list mailing list