windows PyQt4中exe的窗口图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2312210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Window Icon of Exe in PyQt4
提问by realz
I have a small program in PyQt4 and I want to compile the program into an Exe. I am using py2exe to do that. I can successfully set icon in the windows title bar using the following code, but when i compile it into exe the icon is lost and i see the default windows application. here is my program:
我在 PyQt4 中有一个小程序,我想将程序编译成一个 Exe。我正在使用 py2exe 来做到这一点。我可以使用以下代码在 windows 标题栏中成功设置图标,但是当我将其编译为 exe 时,图标丢失了,我看到了默认的 windows 应用程序。这是我的程序:
import sys
from PyQt4 import QtGui
class Icon(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Icon')
self.setWindowIcon(QtGui.QIcon('c:/python26_/repy26/icons/iqor1.ico'))
app = QtGui.QApplication(sys.argv)
icon = Icon()
icon.show()
sys.exit(app.exec_())
**** Here is the setup.py for py2exe****
**** 这里是 py2exe 的 setup.py****
from distutils.core import setup
import py2exe
setup(windows=[{"script":"iconqt.py"
,"icon_resources": [(1, "Iqor1.ico")]}]
,options={"py2exe":{"includes":["sip", "PyQt4.QtCore"]}})
回答by Jesse Aldridge
The problem is that py2exe doesn't include the qt icon reader plugin. You need to tell it to include it with the data_files parameter. Something along these lines:
问题是 py2exe 不包含 qt 图标阅读器插件。您需要告诉它使用 data_files 参数包含它。沿着这些路线的东西:
setup(windows=[{"script":script_path,
"icon_resources":[(1, icon_path)]}],
data_files = [
('imageformats', [
r'C:\Python26\Lib\site-packages\PyQt4\plugins\imageformats\qico4.dll'
])],
options={"py2exe":{"packages":["gzip"],
"includes":["sip"]}})
回答by swanson
I believe you need to reference the .ico
file directly from the EXE or DLL that you are creating with py2exe
. You seem to have the setup.py script correct, so take a look at: http://www.py2exe.org/index.cgi/CustomIcons. There is an example for wxWidgets, but you could try to adapt it to Qt.
我相信您需要.ico
直接从您使用py2exe
. 您的 setup.py 脚本似乎正确,因此请查看:http: //www.py2exe.org/index.cgi/CustomIcons。wxWidgets 有一个示例,但您可以尝试将其调整为 Qt。
回答by Andy M
I would suggest you to create a file called YourApp.rc, add up the following line :
我建议您创建一个名为 YourApp.rc 的文件,添加以下行:
IDI_ICON1 ICON DISCARDABLE "res/icons/app_icon.ico"
Then in your .PRO file, add up the following lines :
然后在您的 .PRO 文件中,添加以下几行:
win32{
RC_FILE = YourApp.rc
}
It should fix your problem !
它应该可以解决您的问题!
回答by marcus trenton
I had the same issue. For some reason it worked just fine with a image.png file & not an image.ico file. No clue why. But i converted the ico to png & it worked
我遇到过同样的问题。出于某种原因,它对 image.png 文件而不是 image.ico 文件工作得很好。不知道为什么。但是我将 ico 转换为 png 并且它起作用了