Tk's default Toplevel - counterintuitive

Guido van Rossum guido at cnri.reston.va.us
Fri Aug 20 08:27:16 EDT 1999


bowman <bowman at montana.com> writes:

> Chad Netzer wrote:
> > 
> > PS.  What is the way to register a callback so that you can intercept the
> > window manager's request to destroy a window?  Then you could just bind
> > the sys.exit call to this on the Tk object...
> 
> def main():
> 	global top
> 	top = Tk()
> 	top.withdraw()
> 	
> 	mb = buildMenubar()
> 	mb.entryconfigure(2, activebackground='red')
> 	myTop = Toplevel(menu=mb)
> 	myTop.bind('<Destroy>', dying)
> 
> 	
> def dying(event) :
> 	print 'Toplevel is getting destroyed'
> 	top.quit()
> 
> 
> The above snippet is a little ugly, but it works. Otherwise, if you kill
> the visible window with the system box, the top process keeps on
> chugging along. I like the Toplevel's menu setup rather than using
> Menubutton's and the deprecated (atleast in the Tcl/Tk world)
> tk_menuBar.

Actually, the accepted idiom (all over the place in code I write) is

        toplevel_window.protocol("WM_DELETE_WINDOW", close_callback)

The close_callback function takes no arguments.  In simple cases, you
can use

	root.protocol("WM_DELETE_WINDOW", root.destroy)

or

	root.protocol("WM_DELETE_WINDOW", root.quit)

--Guido van Rossum (home page: http://www.python.org/~guido/)




More information about the Python-list mailing list