python - PyInstaller 图标选项在 Mac 上不起作用

标签 python macos icons pyinstaller icns

我在我的 mac 上运行了以下命令并创建了一个 .app 文件。

pyinstaller --icon icon.icns --noconsole -n testApp main.py

但是,生成的 .app 文件不显示图标。

enter image description here

icon.icns 被指定为 info.plist 中的图标文件。

enter image description here

Content/Resouces 文件夹包含 icon.icns。

enter image description here

当我运行 .app 文件时,我在 Dock 上看到一个图标。

但是,该图标不会反射(reflect)在 .app 文件本身中。为什么是这样?

最佳答案

您应该使用命令来创建 macOS 应用程序包
如果指定正确,icon.icns将被复制到 Resources 文件夹并应用于 testApp.app捆绑在 dist目录,运行 pyinstaller 命令后。 (不要按照建议尝试 .ico,这是针对 Windows 的)。
我只是尝试使用 PySide2 创建一个简单的 Python 应用程序,它在应用程序包中包含了图标文件,没有任何问题。
鉴于 icon.icns 位于同一目录 main.py :
你的命令应该是:$ pyinstaller --onefile --windowed --icon icon.icns --name testApp main.py这将生成应用程序包,您应该能够正常运行它。

但是等等,还有更多……
遗憾的是,故事到这里还没有结束。
剧透:
要以“正确的方式”做事并使 macOS 满意,您还应该包括:--osx-bundle-identifier 'YOUR_IDENTIFIER'这样,规范文件将包含 Info.plist 的信息。生成的文件。
在规范文件中也应该有一个部分描述 Info.plist内容,在以下几行:

app = BUNDLE(exe,
         name='testApp.app',
         icon='icon.icns',
         bundle_identifier='com.youridentifier',
         info_plist={
            'NSPrincipalClass': 'NSApplication',
            'NSAppleScriptEnabled': False,
            'CFBundleDocumentTypes': [
                {
                    'CFBundleTypeName': 'My File Format',
                    'CFBundleTypeIconFile': 'MyFileIcon.icns',
                    'LSItemContentTypes': ['com.example.myformat'],
                    'LSHandlerRank': 'Owner'
                    }
                ]
            },
         )

在上面的例子中,键/值'NSPrincipalClass': 'NSApplication'允许 Mac OS X 使用视网膜分辨率渲染应用程序是必要的。
key 'NSAppleScriptEnabled'被分配了 Python bool 值 False,它将作为 <false/> 输出到 Info.plist .最后是 key CFBundleDocumentTypes告诉 Mac OS X 您的应用程序支持哪些文件类型(如果有)。

关于python - PyInstaller 图标选项在 Mac 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62451711/

相关文章:

python - 将蒙版图像保存为 FITS

java - Java 7 和 Java 8 可以在 OSX 上共存吗

macos - 在 Visual Studio 2017 for Mac 中更改行结尾

python - 如何显示 basemap 的颜色条

javascript - Selenium +火狐 : empty execute_script arguments

icons - 从 Android Studio 中的图像重建或重新生成 'ic_launcher.png'

html - 基础图标字体 :hover styling not working, sass/css

python - 在 Matplotlib 图形窗口中更改图标

python - 每个转换器以浮点形式读取数据

java - Info.plist 表示是 Mac app,但是用 Application Loader 提交时提交的是 ipa