__del__() in a Tkinter app

Fredrik Lundh fredrik at effbot.org
Sat Dec 16 14:03:53 CET 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, ...):
            # not really needed in 2.0 and later, but it doesn't hurt
            self.protocol("WM_DELETE_WINDOW", self.destroy)

        def destroy(self):
            ... handle shutdown


More information about the Python-list mailing list