[Tkinter-discuss] how to delete a toplevel window from within it?

Stewart Midwinter stewart.midwinter at gmail.com
Fri Jan 7 21:56:44 CET 2005


I've put together a little text editor widget that's run from my app
in order to edit config files.  This widget is in its own module, so I
import the module and create an instance of the FileEditor class in a
Toplevel window.  All is well with my widget; it opens files, allows
simple edits to take place, and can do Save or Save As. I'll be glad
to share my recipe if anyone is interested.

My quandary is how to close this Toplevel window.  It's a simple thing
to add a close command when I create the Toplevel. For instance, I can
do the following in my app:

        winE = Toplevel()
        winE.title('File Editor')    
        exitButton = Button(winE, text = 'Close', command = winE.destroy)
        exitButton.pack(side = 'bottom')
        widget = FileEditor.Editor(winE, location, filename)

but I wonder if it is possible, or advisable, or more elegant, to have
a button inside the FileEditor module itself that knows how to delete
its parent.  If it is possible, how I would I do it?  I tried the
following method, invoked by a button, without success

# in FileEditor.py:
...
    def close(self):
        '''close the window that we live in'''
        self.destroy()
...
but the following error is returned:
AttributeError: Editor instance has no attribute 'destroy'


thanks,
-- 
Stewart Midwinter
stewart at midwinter.ca
stewart.midwinter at gmail.com


More information about the Tkinter-discuss mailing list