赞
踩
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource>
</qresource>
</RCC>
<RCC>
<qresource prefix="pic">
<file>图片.jpg</file>
</qresource>
</RCC>
pixmap
属性,单击后面的按钮,把它的值改为资源文件中的一张图片,如下图:# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'D:\Python--PyQt5--Eric6\fir1.ui' # # Created by: PyQt5 UI code generator 5.13.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(693, 608) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(190, 180, 211, 161)) self.label.setText("") self.label.setPixmap(QtGui.QPixmap(":/pic/图片.jpg")) self.label.setObjectName("label") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) import apprcc_rc if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_())
运行该代码,会抛出以下异常信息:
Traceback (most recent call last):
File “D:\Python–PyQt5–Eric6\Ui_fir1.py”, line 29, in
import apprcc_rc
ModuleNotFoundError: No module named ‘apprcc_rc’
说明在脚本中使用以下代码导入的模块异常,所以我们还需要将.qrc文件转换为.py文件。
pyrcc5 apprcc.qrc -o apprcc_rc.py
。完成之后就可以正常运行了,效果如下图:
(注:最好是将资源文件、.ui文件和.qrc文件保存到同一路径下面)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。