如何使用 Pyinstaller 打包的 Kivy Python 应用程序在 OSX 菜单栏中创建菜单项?

2022-01-15 00:00:00 python kivy pyinstaller macos

问题描述

为了在我的 Pyinstaller 打包的 Kivy Python 应用程序中实现原生感,我想在 OSX 菜单栏中创建菜单项.如果使用 PyQt,我可以轻松实现这一点.但是由于我使用的是 Kivy,我似乎不得不求助于其他方式.

To achieve sense of nativeness in my Pyinstaller packaged Kivy Python application, I would like to create menu item in OSX menu bar. If using PyQt, I can easily achieve this. But as I am using Kivy, it seems like I have to resort to other ways.

有什么建议吗?我猜它会在 PyObjc 或 AppleScript 的范围内.不过不确定.

Any suggestion? I guess it will be in the range of PyObjc or AppleScript. Not sure though.


解决方案

我想这更像是 Pyinstaller 的问题,它是 Kivy 的问题.

I guess this is more to Pyinstaller question that it is as Kivy question.

两者都不是:)

PyInstaller 只是将 python 应用程序包装到一个看起来像本机的 OS X 应用程序包中.
由于 Kivy 无法在 OS X 中创建菜单栏图标,因此您必须坚持使用 PyObjC.

PyInstaller just wraps the python application into a native-looking OS X application bundle.
Since Kivy doesn't have the ability to create a menu bar icon in OS X, you have to stick with PyObjC.

首先,您必须安装 Xcode(如果尚未安装).您可以从 App Store 或 Apple 的开发者中心 下载它.

First, you've got to install Xcode (if not installed yet). You can download it from the App Store or from the Apple's Developer Center.

然后安装 PyObjC 本身:

Then install PyObjC itself:

pip install pyobjc

已经有一些例子了(first, another) 关于使用 PyObjC 创建状态栏图标.

There are already some examples (first, another) on creating a status bar icon using PyObjC.

还有一个很棒的库,叫做 rumps,它极大地简化了代码需要制作状态栏图标、菜单和操作.也许它可以帮助您进行状态栏交互.

There's also an awesome library called rumps which greatly simplifies the code needed to make status bar icons, menus and actions. Perhaps it may help you with status bar interacting.

相关文章