开发者

Apple Python launcher is acting on command key bindings

开发者 https://www.devze.com 2023-02-17 20:56 出处:网络
I\'ve written a Python utility that uses tkinter. I\'m running it on a Macintosh. When it is executed, it runs within an apple-supplied Python launcher program (/Library/Frameworks/Python.framework/Ve

I've written a Python utility that uses tkinter. I'm running it on a Macintosh. When it is executed, it runs within an apple-supplied Python launcher program (/Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app).

My code installs its own menus and I bind to the usual Macintosh command-key equivalents for my edit menu (Command-x, command-c, command-x, command-a, command-z) and for quitting (command-q). My problem is that the Python launcher program is responding to the command key bindings. This is inconvenient for things like pasting because it gets done twice. It's a real problem with quitting because the launcher program kills my program before I can save changed files.

Is there some way I can stop the Python launcher program from acting on command key equivalents? I attempted this: "rootWindow.unbind ('<Command-Key-q>')", but to no avail. The launcher program quits before my code can clean up.

I'm using CPython 3.2开发者_运维问答 on OS X 10.6.6.


Instead of overriding Tkinter's default key bindings, consider re-mapping Tcl's "exit" command to a custom function. (This is called every time you hit command-q or use the "quit" menu item.)

def save_and_exit():
    save_changed_files()
    sys.exit()

self.createcommand('exit', save_and_exit)

Besides that, I would recommend removing your copy/paste custom keybinds and letting the library do the work for you. If you're still hell-bent on overriding the defaults, Effbot has a nice tutorial on Tkinter events and bindings.


Is there a specific reason why you are using Python.app for launching? This .app is most likely the reason for misbehaving shortcuts.

If I have understood correctly, this launcher is just a wrapper for default python (/usr/bin/python) with special imports.

If you run from terminal (-v is the key here):

/Library/Frameworks/Python.framework/Versions/5.1.1/Resources/Python.app/Contents/MacOS/Python -v

You will see what it imports at the beginning. Adding these lines to your main file should make the command line launching the same as with the .app.

Note also that python.app is in version 5.1.1.

br,

Juha


First off, /Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app is not Apple-supplied. Most likely you installed Python 3.2 using one of the python.org installers here or from some third-party distributor or possibly you built a framework version from source. In any case, Python.app is a dummy application bundle included in each framework version. Its purpose is to ensure that when you invoke python, even from a command line, it is seen by OS X as a full-fledged GUI application. This is particularly important when using tkinter. The default menus and keybindings you see are supplied by Tcl/Tk, not tkinter. As you've discovered, the right way to go about changing these are to remap the default menus. Be aware that there are currently at least three major variants of Tk available on Mac OS X: Aqua Carbon Tk, Aqua Cocoa Tk, and X11 Tk. There are important details, especially with regards to Mac OS X 10.6, about Python and Tcl/Tk on Mac OS X at the python.org website.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号