编译后的代码不使用pyqt4中的qrc加载图像

发布于 2024-11-10 15:21:10 字数 1830 浏览 1 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷鸟归林 2024-11-17 15:21:10

您的问题的解决方案可以在本页底部找到。

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...

惜醉颜 2024-11-17 15:21:10

它是Python QRC 吗?

.qrc 文件动态重新生成代码:

Makefile

all:
    rm -fv *.pyc
    pyuic4 -i 2 -o gui.py gui.ui
    pyrcc4 -o resources_rc.py resources.qrc
    python2 main.py

我的应用程序有一个 Makefile,它可以从 .ui 文件和Qt Designer,您指定资源文件。对我来说,它是resources.rc。但 Python 将 . 更改为下划线,因此您必须创建一个扩展名为 .pyresources_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:

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.

辞慾 2024-11-17 15:21:10

我今天遇到了同样的问题。问题很简单,py2exe 无法识别“jpg”格式的图像。您的背景图片必须是“png”文件。您可以简单地通过使用 Paint 修改 yout 图像的格式来更改它,然后将此图像添加到您的“resourcefilename.qrc”中。然后你重新编译它。不要忘记将 python 代码中的链接从 : 更改

QtGui.QDialog.setStyleSheet(_fromUtf8("QDialog{background-image: url(:/image/**'imagename'.jpg**);}"))

QtGui.QDialog.setStyleSheet(_fromUtf8("QDialog{background-image: url(:/image/**'imagename'.png**);}"))

然后,您只需使用 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 :

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文