Apple Python 启动器作用于命令键绑定

发布于 2024-10-25 03:16:27 字数 544 浏览 2 评论 0原文

我编写了一个使用 tkinter 的 Python 实用程序。我在 Macintosh 上运行它。当它被执行时,它在苹果提供的Python启动程序(/Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app)中运行。

我的代码安装了自己的菜单,并绑定到编辑菜单(Command-x、command-c、command-x、command-a、command-z)和退出(command-q)的常用 Macintosh 命令键等效项。我的问题是 Python 启动程序正在响应命令键绑定。这对于粘贴之类的事情来说很不方便,因为它需要完成两次。退出是一个真正的问题,因为启动程序在我保存更改的文件之前杀死了我的程序。

有什么方法可以阻止 Python 启动程序对等效命令键执行操作吗?我尝试这样做:“rootWindow.unbind ('')”,但无济于事。在我的代码清理之前启动程序就退出了。

我在 OS X 10.6.6 上使用 CPython 3.2。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

五里雾 2024-11-01 03:16:27

不要覆盖 Tkinter 的默认键绑定,而是考虑重新映射 Tcl 的“退出” " 命令 到自定义函数。 (每次点击 command-q 或使用“退出”菜单项时都会调用此函数。)

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

self.createcommand('exit', save_and_exit)

除此之外,我建议删除复制/粘贴自定义按键绑定并让库为您完成工作。如果您仍然一心想要覆盖默认设置,Effbot 在 上有一个很好的教程Tkinter 事件和绑定

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.

櫻之舞 2024-11-01 03:16:27

您使用 Python.app 进行启动有什么具体原因吗?这个 .app 很可能是快捷方式行为不当的原因。

如果我理解正确的话,这个启动器只是带有特殊导入的默认 python (/usr/bin/python) 的包装器。

如果您从终端运行(-v 是此处的关键):

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

您将在开始时看到它导入的内容。将这些行添加到主文件中应该使命令行启动与 .app 相同。

另请注意,python.app 的版本为 5.1.1。

br,

尤哈

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

无声静候 2024-11-01 03:16:27

首先,/Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app 不是 Apple 提供的。您很可能使用 python.org 安装程序之一安装了 Python 3.2 或从某些第三方发行商或者可能您从源代码构建了一个框架版本。无论如何,Python.app 是每个框架版本中包含的虚拟应用程序包。其目的是确保当您调用 python 时,即使是从命令行,OS X 也会将其视为成熟的 GUI 应用程序。这在使用 tkinter 时尤其重要。您看到的默认菜单和键绑定是由 Tcl/Tk 提供的,而不是 tkinter 提供的。正如您所发现的,更改这些的正确方法是重新映射默认菜单。请注意,目前 Mac OS X 上至少有 3 个主要的 Tk 变体:Aqua Carbon Tk、Aqua Cocoa Tk 和 X11 Tk。有一些重要的细节,特别是关于 Mac OS X 10.6,关于 Mac 上的 Python 和 Tcl/Tk OS X 位于 python.org 网站。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文