How to kill an object? (or Debugging reference counts)

Randall Hopper aa8vb at vislab.epa.gov
Tue May 4 12:08:03 EDT 1999


I've attached a simple Tk script that illustrates the problem.

How do I kill "group"?

Something is hanging onto it for dear life and won't let go.
I want to delete group (a PlaceableGroup instance), and let it delete its
Tk representation.

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