[Tkinter-discuss] Redefine quit menu item on Mac to quit from a Twisted/Tkinter application?

Kevin Walzer kw at codebykevin.com
Thu Apr 9 01:02:20 CEST 2009

> So I would like to find some way to modify the behavior of the MacOS X 
> Aqua Tcl/Tk Quit menu item.


This requires some Tcl code.

According to the Tcl/Tk Aqua FAQ at http://wiki.tcl.tk/12987, Command-Q 
(and the Quit menu item) trigger the kAEQuitApplication event, which is 
mapped to Tcl's "exit" (identical to sys.exit)  command.

The way to change this is to map the "exit" command to something else. 
This is so trivial to do in Tcl that it's actually dangerous (I've had 
apps crash on me because I named an image "exit").  I did a little 
experimentation in Wish, with this code:

proc exit {} {
	puts "foo"

After entering this code during a Wish session in  Terminal, hitting 
"Command-Q" printed "foo" to standard output. The only way to quit at 
that point was to close the toplevel window.

The way to create custom Tcl commands from Python is with the 
"createcommand" function. I use this function to make the "preferences" 
menu item visible in my applications:

         self.createcommand('::tk::mac::ShowPreferences', self.setPrefs)

Something like this would probably get close to what you're looking for:

def printfoo:
	print "foo"

self.createcommand('exit', printfoo)

In other words, this should override the hard-coded "quit" function by 
mapping the "exit" Tcl command to something else. Then you could add 
your own event handler for Command-Q.

If that doesn't work, I'm not sure what to suggest. This is probably 
more drilling down into Tcl's internals than you wanted, anyway.


Kevin Walzer
Code by Kevin

More information about the Tkinter-discuss mailing list