Tkinter Canvas Items - not ref counted?

Randall Hopper aa8vb at vislab.epa.gov
Tue May 4 06:38:20 EDT 1999


Charles G Waldman:
 |Randall Hopper writes:
 | >      Why does the Canvas Rectangle and its canvas item not get destroyed
 | > using the following fragment:
...
 | >  rect = Rectangle( canvas, 1, 1, 100, 100 )
 | >  rect = None                                  # <--- Destroy it here
...
 |The line "rect = Rectangle( canvas, 1, 1, 100, 100)"
 |causes a rectangle to be created (the call to Rectangle) and it's ID
 |to be stored in the variable "rect".  Subsequently doing "rect = None" 
 |gets rid of the ID but doesn't undo the effect of creating the
 |rectangle - you've just deleted it's handle, not the object itself.
 |
 |Part of the reason this is confusing has to do with the fact that
 |Python and Tk each have their own object hierarchy.
 |
 |If you want to erase the rectangle drawn on the canvas, try:
 |
 | canvas.delete(rect)

Thanks.  It doesn't seem completely intuitive that creating a Tk "shadow"
class creates a widget or canvas item, but deleting the Tk shadow class
doesn't delete the widget or canvas item.  But I'll accept that as how it
works.

Randall





More information about the Python-list mailing list