Python PyInstaller并包含窗口图标 [英] Python PyInstaller and include window icon
问题描述
我已经使用 self.setWindowIcon(QtGui.QIcon('icon.png'))
为我的PyQt应用程序设置了图标,当我运行我的代码时,它运行正常在PyCharm。
I have set the icon for my PyQt application using self.setWindowIcon(QtGui.QIcon('icon.png'))
and it works fine when I run my code in PyCharm.
接下来我使用PyInstaller将我的应用程序转换为一个文件:
Next I converted my application to one file with PyInstaller:
pyinstaller.exe --onefile --windowed opc.py --name myapps
但是,运行可执行文件时,不会显示图标。我做错了什么?
However, when running the executable the icon is not shown. What am I doing wrong ?
来自PyCharm的左站点代码,在一个文件的右侧站点上(pyinstaller.exe - -onefile --windowed opc.py --name myapps)。
为什么不一样?
我想* .png图标,因为它是透明的。
On left site code from PyCharm, on right site from one file (pyinstaller.exe --onefile --windowed opc.py --name myapps). Why is not the same ? I want *.png icon because is transparent.
推荐答案
在Windows上运行可执行文件时显示的图标来自可执行文件本身。要将图标与应用程序捆绑在一起,您需要在使用 pyinstaller.exe
构建时通过传递 - icon
参数。例如:
The icon displayed when running an executable on Windows comes from the executable file itself. To bundle an icon with your application you need to specify the icon when building with pyinstaller.exe
by passing the --icon
parameter. For example:
pyinstaller.exe --onefile --windowed --name myapps --icon=icon.ico opc.py
请注意,与 setWindowIcon()
不同图标文件必须是 .ico
格式,因此您需要先从 .png
转换它。
Note that unlike for setWindowIcon()
the icon file must be in .ico
format, so you will need to convert it from the .png
first.
如果你想使用PyQt调用来设置图标,你需要将图标文件捆绑到可执行文件中,这可以使用PyInstaller spec文件。有关创建和修改规范文件的过程的演练,请参见之前的答案。
If you want to use the PyQt call to set the icon you will need to bundle the icon file into the executable, which can be done using a PyInstaller spec file. A walkthrough of the process of creating and modifying the spec file is in this previous answer.
这篇关于Python PyInstaller并包含窗口图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!