Are tk derived objects ever destroyed?

richard_chamberlain richard_chamberlain at ntlworld.com
Fri Jun 30 17:21:45 EDT 2000


Hi Jerome,

This is a tricky question ;-)

Unless you explicitly call destroy against your class it doesn't seem to
call ___del__. You can get round it in the following manner:

#!/usr/bin/python
from Tkinter import *
class Frm(Toplevel):
    def __init__(self):
        Toplevel.__init__(self)
        Label(self, text='spam').pack()
        self.bind('<Destroy>',self.kill)
    def kill(self,event):
        self.destroy()
    def __del__(self):
        print 'Kai kai kai'
Button(text='create', command=Frm).pack()
mainloop()

Richard


Jerome Quelin <jerome.quelin at insalien.org> wrote in message
news:962360149.1278823422 at news.libertysurf.fr...
> Hi all,
>
> Look at the snippet code below:
>
> -- File toplevel_creator.py
> #!/usr/bin/python
> from Tkinter import *
> class Frm(Toplevel):
>     def __init__(self):
>         Toplevel.__init__(self)
>         Label(self, text='spam').pack()
>     def __del__(self):
>         print 'Kai kai kai'
> Button(text='create', command=Frm).pack()
> mainloop()
> -- End of file
>
> When running this code and creating some toplevel windows with the
> create button, the objects aren't destroyed (the __del__ function
> doesn't get called) when I destroy the window.
> I wonder why, since there aren't any other references to this toplevel
> window. Unless Tkinter keeps tracks of all widgets created? Or
> destroying the widgets does not clears all references to the widget?
> Then what must I do to destroy the object when the window gets
> destroyed?
>
> Thanks,
> Jerome
> --
> jerome.quelin at insalien.org
>





More information about the Python-list mailing list