编译后的代码不使用pyqt4中的qrc加载图像
我使用 pyqt4 作为 GUI 创建了一个程序,我使用资源文件或 .qrc 来加载背景图像。 我的问题是当我使用 py2exe 编译它时,背景图像未加载。
这是示例代码。
GUI 模块:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(563, 319)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setStyleSheet(_fromUtf8("background-image: url(:/test/mr_bean_kid.jpg);"))
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 563, 18))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
import ss_rc
主模块:
from sample import Ui_MainWindow
from PyQt4 import QtCore, QtGui
class test(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
myapp = test()
myapp.show()
sys.exit(app.exec_())
我不会包含 qrc,因为它太长了。 谢谢。
Ive created a program using pyqt4 as GUI Ive used resource file or .qrc to load background image.
My problem is when i compiled it using py2exe, the background image is not loaded.
here is the sample code.
GUI MODULE:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(563, 319)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setStyleSheet(_fromUtf8("background-image: url(:/test/mr_bean_kid.jpg);"))
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 563, 18))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
import ss_rc
MAIN MODULE:
from sample import Ui_MainWindow
from PyQt4 import QtCore, QtGui
class test(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
myapp = test()
myapp.show()
sys.exit(app.exec_())
i will not include the qrc because its to long.
thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题的解决方案可以在本页底部找到。
http://www.py2exe.org/index.cgi/Py2exeAndPyQt
尝试此解决方案时,确保您没有将所有文件捆绑到一个可执行文件中,这将不起作用......
The solution to your problem can be found at the bottom of this page.
http://www.py2exe.org/index.cgi/Py2exeAndPyQt
When trying this solution, make sure you are not bundling all files into a single executable, that won't work...
它是Python QRC 吗?
.qrc 文件动态重新生成代码:
Makefile:
我的应用程序有一个 Makefile,它可以从
.ui
文件和Qt Designer,您指定资源文件。对我来说,它是
resources.rc
。但 Python 将.
更改为下划线,因此您必须创建一个扩展名为.py
的resources_rc
文件。Is it a Python QRC?
I have a Makefile for my application, which dynamically re-generates the code from the
.ui
files and the.qrc
file:Makefile:
In Qt Designer, you specify the resource file. For me, it was
resources.rc
. But Python changes that.
to an underscore, so you have to create aresources_rc
file with the.py
extension.我今天遇到了同样的问题。问题很简单,py2exe 无法识别“jpg”格式的图像。您的背景图片必须是“png”文件。您可以简单地通过使用 Paint 修改 yout 图像的格式来更改它,然后将此图像添加到您的“resourcefilename.qrc”中。然后你重新编译它。不要忘记将 python 代码中的链接从 : 更改
为
然后,您只需使用 py2exe 重新编译代码,现在您应该有了背景。
I encountered the same issue today. The problem is simply that py2exe does not recognize "jpg" format image. Your background image has to be a "png" file. You can simply change that by modifying the format of yout image with paint for instance and then add this image to your "resourcefilename.qrc". Then you recompile it. Do not forget to change the link in your python code from :
to
Then, you just have to recompile your code with py2exe and you should now have your background.