Compiled code does not load image using qrc in pyqt4
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.central开发者_开发问答widget.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.
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...
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:
all:
rm -fv *.pyc
pyuic4 -i 2 -o gui.py gui.ui
pyrcc4 -o resources_rc.py resources.qrc
python2 main.py
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 a resources_rc
file with the .py
extension.
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 :
QtGui.QDialog.setStyleSheet(_fromUtf8("QDialog{background-image: url(:/image/**'imagename'.jpg**);}"))
to
QtGui.QDialog.setStyleSheet(_fromUtf8("QDialog{background-image: url(:/image/**'imagename'.png**);}"))
Then, you just have to recompile your code with py2exe and you should now have your background.
精彩评论