__del__() in a Tkinter app

Fredrik Lundh fredrik at effbot.org
Sat Dec 16 08:03:53 EST 2000


"Richard" wrote:
> I have tried adding a __del__() method to a Tkinter application so it can
> remove temporary files when the program exits.

Don't ever do things like that in a __del__ method.   Python isn't
C++, and doesn't make any guarantees that __del__ methods will
ever be called.

To properly shut down a Tkinter application, bind the "<Destroy>"
event on the root/toplevel window.

You can also overload the destroy method:

    class myGUI(Tk):

        def __init__(self, ...):
            Tk.__init__(self)
            # not really needed in 2.0 and later, but it doesn't hurt
            self.protocol("WM_DELETE_WINDOW", self.destroy)

        def destroy(self):
            Tk.destroy(self)
            ... handle shutdown

</F>





More information about the Python-list mailing list