Tkinter problem on Mac OS X

A

Andrew Trevorrow

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
 
J

James Stroud

Andrew said:
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/
 
R

Russell E. Owen

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.

I am pretty sure it's the underlying Tcl/Tk that's causing you grief.

Does your application itself use Tcl/Tk anywhere? I'm guessing not, as
I'd expect you'd already have gone to some trouble to get the menus
right.

Unfortunately, I can't offer a solution. You may want to post to the
Tkinter mailing list (via news you can use gmane, group
gmane.comp.python.tkinter; I'm not sure if you have to sign up for the
list before posting).

You could import Tcl/Tk yourself "up front" and then fix your
application's menus once. But Aqua Tcl/Tk is painfully slow to start up,
so this could really slow down startup of your own application. (Note
that you'd have to be prepared for Tkinter to be missing (it certainly
is by default on 10.3). You might also have to make users use one
mainloop (see next paragraph) for this to work.

James Stroud also brings up an interesting point--that it could be
dangerous if there are multiple scripts each trying to run their own
mainloop. But if only one runs at a time, and cleans up after itself,
you're probably OK. If that's not the case, you may want to start a Tk
main loop yourself and ask users to use that loop. Again, though--it'll
take time, and it'll only help those users who use Tkinter in their
scripts.

-- Russell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top