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

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Tue Jan 11 10:33:18 CET 2005


On Fri, 7 Jan 2005 13:56:44 -0700, Stewart Midwinter  
<stewart.midwinter at gmail.com> wrote:

> 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,

I take it the FileEditor.Editor class does not inherit from a Tkinter  
widget
(Frame for example...)  in any case the first argument you pass to the
Editor is the name of the Toplevel window so that binding the destroy  
method
of this windget shouldbe easy like so:


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


## in FileEditor


class Editor:
     def __init__(self, parent, location, filename):
         ...
         init stuff...
         ...
         eb = Button(self.toolbar, text="Destroy", command=parent.destroy)
         eb.pack()


You could also try inheriting from Toplevel - somthing I do all the time
especially if I reuse the same instance of a class - (in this case I  
withdraw
the window - not destroy, then deiconify it when I need to use it again  
with new
data):-

class Editor(Toplevel):
     def __init__(self, mainapp, location=None, filename=None):
         Toplevel.__init__(self, mainapp)
         self.withdraw()
         self.protocol("WM_DELETE_WINDOW", self.withdraw
         self.title("Editor Window")
         self.location = location
         ...
         rest of init stuff
         ....

     def show(self, location, filename):
         self.location = location
         self.filename = filename
         ## read filename in etc...
         self.setup()
         ## show myself
         self.deiconify()
         self.tkraise()

## then in calling class / module


class MainApp(Tk)
     def __init__(self):
         Tk.__init__(self)
         self.title("Main Window")
         ## editor window starts but does not show up yet...
         self.editor = Editor(self, location="", filename"")



     def edit(self):
         self.editor.show(location="da da",  
filename="/some/file/I/need/to/edit")




Just an idea... I use this a lot
Martin

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/



More information about the Tkinter-discuss mailing list