Tkinter problem on Mac OS X

James Stroud jstroud at ucla.edu
Sun Apr 9 23:44:45 EDT 2006


Andrew Trevorrow wrote:
> Our app uses embedded Python to allow users to run arbitrary scripts.
> Scripts that import Tkinter run fine on Windows, but on Mac OS X there
> is a serious problem.  After a script does "root = Tk()" our app's menus
> are permanently changed in the following way:
> 
> - The top item in the application menu changes to "About Tcl & Tk...".
> - The Quit item is disabled.
> - The File and Edit menus are completely replaced.
> - All further menus (except Help) are removed.
> 
> Is there a way to prevent Tkinter clobbering our app's menus?
> Or perhaps a way to restore them after the root.mainloop() call?
> 
> I've read Fredrik Lundh's excellent tutorial on Tkinter and done a lot
> of googling but haven't been able to find a solution.
> 
> I can probably add some Mac-specific code to detect a menu change after
> a script ends and then rebuild our menus, but I'm hoping there's a
> simpler Tkinter (or Tcl?) solution.
> 
> Andrew

This menu mashing is a problem. This really has nothing to do with 
programing in Tcl, Tk, Python, or Tkinter. First of all, the "About Tcl 
& Tk" is hard-wired during the build of Aqua Tk. To fix this will 
require downloading the sources and building your own Aqua Tk from 
scratch: http://tcltkaqua.sourceforge.net/8.4.10/

Alternatively, you can res-edit the appropriate files. The "About Tcl & 
Tk" menu item is held in an rsrc file. I forget exactly where, but I can 
take a look on my ibook if you are interested in this route. To do this, 
you will want a working copy of resknife. I had some trouble with it, so 
I had to build it for my ibook with xcode--IIRC, the build I downloaded 
had problems saving. Getting the source and building on my computer 
fixed it. Its a great program.

I'm guessing the best way to get around destroying your menus is to 
intercept calls to Tk(). I've had bad luck with multiple Tk() instances 
running in the same session (this is the case with EVERY implementation 
of Tkinter I've seen, Linux, Mac, etc.). StringVar and IntVar instances 
get very screwy with multiple Tk()s. You might want to re-define Tk() to 
return the root instance created by your program. Also, knew Toplevel()s 
will have their own menus, resulting in your application's menus 
becoming replaced while the Toplevel() is in the foreground, that's just 
how Aqua Tk works. This may be something you have to live with if you 
want your students (I'm guessing) to learn about python in your environment.

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list