__del__() in a Tkinter app

Richard richard at NOstarfighterSPAM.freeuk.com
Sat Dec 16 06:24:18 EST 2000


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

However the __del__() method is never called, as in this simple example:

class GUI:
    def __init__(self):
        self.root = Tk()

        quit_pb = Button(self.root, text="Quit")
        quit_pb.bind("<Button>", self.quit)
        quit_pb.pack(side=LEFT)

        self.root.mainloop()

    def __del__(self):
        print 'GUI destructor called'

    def quit(self, event=None):
        self.root.quit()

class NonGUI:
    def __del__(self):
        print 'NonGUI class destructor called'

if __name__ == '__main__' :

    g = GUI()
    print 'after creating GUI object'

    n = NonGUI()
    print 'after creating NonGUI object'

When the app is run and the Quit button is pressed, this produces:

NonGUI class destructor called

i.e. the destructor in the NonGUI object is called, but the destructor in
the GUI object is not.

Does anybody know why this happens? Is it possible to fix it so the GUI
object destructor is called?

Richard






More information about the Python-list mailing list