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

tai*_*chi 4 python macos icons pyinstaller icns

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

pyinstaller --icon icon.icns --noconsole -n testApp main.py
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

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

在此处输入图片说明

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

在此处输入图片说明

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

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

cse*_*der 9

您应该使用该命令来创建 macOS 应用程序包

如果指定正确,icon.icns将在运行 pyinstaller 命令后复制到 Resources 文件夹并应用于目录中的testApp.appdist。(不要按照建议尝试 .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'
                    }
                ]
            },
         )

Run Code Online (Sandbox Code Playgroud)

在上面的示例中,键/值 'NSPrincipalClass': 'NSApplication' 是允许 Mac OS X 使用视网膜分辨率渲染应用程序所必需的。

'NSAppleScriptEnabled'被分配了 Python 布尔值 False,它将作为 Info.plist 输出<false/>。最后,keyCFBundleDocumentTypes告诉 Mac OS X 您的应用程序支持哪些文件类型(如果有)。