我对PyQt5完全陌生,我想在与设计人员创建的“带有按钮的对话框”窗口中插入图像(我不知道这是否会有所影响,所以我提到它)
我按照this stackoverflow answer给我的指示,我做了如下操作:
我创建的所有文件(Test.qrc,图像和.ui文件)都在该文件夹中
然后我使用.ui
将生成的.py
转换为pyui5.py
,然后在运行生成的文件时,在控制台上弹出以下错误消息
File "C:\Users\MyUserName\Desktop\Testings\FirstUI.py", line 44, in <module>
import Test_rc
ImportError: No module named 'Test_rc'
当我手动删除该行代码时,会出现其余的UI,只有图像丢失,因此我可以确信地说错误涉及图像
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(391, 178)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(20, 120, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.textBrowser = QtWidgets.QTextBrowser(Dialog)
self.textBrowser.setGeometry(QtCore.QRect(10, 20, 371, 61))
self.textBrowser.setObjectName("textBrowser")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(20, 100, 61, 71))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.textBrowser.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Hello world!</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">This is a test of the current technology we posess in our hands. Whatever we obtain here shall not be shown to any other human being without proper authorization</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">One day this fleeting moment in spacetime will be remembered, or probably not, I am no omnipotent being</p></body></html>"))
self.label.setText(_translate("Dialog", "<html><head/><body><p><img src=\":/PrefixTest/Computer.png\"/></p></body></html>"))
import Test_rc
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())