使用 Pyinstaller 将 window 图标嵌入到 EXE
Embed window icon to EXE with Pyinstaller
我正在使用 Pyinstaller 为我的 wxPython 应用构建一个 Windows 单文件可执行文件。我想添加一个 window 图标,并且仅当我将 test.ico 文件放在构建的可执行文件旁边时,执行 pyinstaller --icon=test.ico --onefile --noconsole test.pyw
才完全没问题。这让我同时分发 exe 和图标,这至少让人不舒服。
我也是
icon = wx.EmptyIcon()
icon.CopyFromBitmap(wx.Bitmap("test.ico", wx.BITMAP_TYPE_ANY))
self.SetIcon(icon)
在我的 wxPython 应用程序中。
我的研究建议对图标的 base64 字符串表示进行硬编码,但它是一个非常长的字符串,因为我还需要在纸上打印我的代码。我看到了 this other post,感觉它有我的答案,但我就是不明白。
所以。如何将图标嵌入到exe中?
编辑:.spec
文件
# -*- mode: python -*-
block_cipher = None
a = Analysis(['test.pyw'],
pathex=['D:\test'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='test',
debug=False,
strip=False,
upx=True,
console=False , icon='test.ico')
首先,使用 --icon
选项,您已经将 .ico
包含在可执行文件中。但是 wxPython 没有办法从 .exe
中提取它(我想使用 windows 系统调用不会那么难,但这不是重点)。
因此您必须第二次包含 .ico
-文件,这次是将其包含在文件系统中。
Whosebug article linked by you已经有了答案:
修改 .spec
文件以将图标文件包含到捆绑的应用程序中。
当运行应用程序时,它会将捆绑文件解压缩到一个临时位置。正如您引用的文章中指出的那样,资源路径在sys._MEIPASS
中指定。在形成完整的文件限定符时,不要忘记考虑子目录(例如 images
代表 ./images/icon.ico
)。
显示您的 .spec
文件修改和检索图标的代码以获得进一步的帮助
我正在使用 Pyinstaller 为我的 wxPython 应用构建一个 Windows 单文件可执行文件。我想添加一个 window 图标,并且仅当我将 test.ico 文件放在构建的可执行文件旁边时,执行 pyinstaller --icon=test.ico --onefile --noconsole test.pyw
才完全没问题。这让我同时分发 exe 和图标,这至少让人不舒服。
我也是
icon = wx.EmptyIcon()
icon.CopyFromBitmap(wx.Bitmap("test.ico", wx.BITMAP_TYPE_ANY))
self.SetIcon(icon)
在我的 wxPython 应用程序中。
我的研究建议对图标的 base64 字符串表示进行硬编码,但它是一个非常长的字符串,因为我还需要在纸上打印我的代码。我看到了 this other post,感觉它有我的答案,但我就是不明白。
所以。如何将图标嵌入到exe中?
编辑:.spec
文件
# -*- mode: python -*-
block_cipher = None
a = Analysis(['test.pyw'],
pathex=['D:\test'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='test',
debug=False,
strip=False,
upx=True,
console=False , icon='test.ico')
首先,使用 --icon
选项,您已经将 .ico
包含在可执行文件中。但是 wxPython 没有办法从 .exe
中提取它(我想使用 windows 系统调用不会那么难,但这不是重点)。
因此您必须第二次包含 .ico
-文件,这次是将其包含在文件系统中。
Whosebug article linked by you已经有了答案:
修改
.spec
文件以将图标文件包含到捆绑的应用程序中。当运行应用程序时,它会将捆绑文件解压缩到一个临时位置。正如您引用的文章中指出的那样,资源路径在
sys._MEIPASS
中指定。在形成完整的文件限定符时,不要忘记考虑子目录(例如images
代表./images/icon.ico
)。
显示您的 .spec
文件修改和检索图标的代码以获得进一步的帮助