Tkinter: cleaning up when a toplevel is destroyed

David R. Smith drs at labs.agilent.com
Thu Nov 15 17:59:41 EST 2001


I want to create multiple gizmos and views of them, letting the views
overlap on the screen.  In my searching, I came across references to an
MS-Win-style multiple document interface widget set, but the links were
all stale.  P.32 of Grayson shows the creation of multiple toplevel
windows, and I figured that would be the ticket.  So I coded that up,
but I have a problem with how to delete a data structure when the user
deletes the associated toplevel window by clicking on its X-button in
the upper right corner.
What I have right now is:

class BlockView:
    def __init__(self, block,    # Display is of a block
                 title,             # for title bar
                 root,             # ur-toplevel interface
                 destroyCB):         # callback for when the window is
destroyed
        self.tl = Pmw.MegaToplevel(root)
        self.tl.bind('<Destroy>',
                     lambda event, self=self, destroyCB=destroyCB:
destroyCB(event,self))
        ...


The idea is that when the MegaToplevel is destroyed, BlockView sends a
'delete me' message to its creator via destroyCB.  The creator then
deletes the BlockView from its lists.

However, Destroy events get sent for all the children of the new
MegaToplevel.  I need to strain out all but one callback.  I thought I
could do it by rewriting the lambda expression as

        lambda event, self=self, destroyCB=destroyCB: event.widget is
self.tl and destroyCB(event,self))

but this never calls destroyCB.  I tried rewriting the new test as

                event.widget is self.tl.interior()

but that didn't activate, either.  Obviously, I don't understand what
event.widget is, and Grayson's explanation of events isn't very
thorough.  Can someone give me some hints here?

     TIA
     David Smith






More information about the Python-list mailing list