【发布时间】:2018-03-29 00:05:04
【问题描述】:
我正在 pyqt5 中构建一个小实用程序,并尝试将一个变量传递给另一个类 ui 文件。
主界面文件
self.showError(errorMessage)
如何将“errorMessage”传递给这个类?我应该这样通过吗? 类 Ui_Error(object, errorMessage):
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Error(object):
def setupUi(self, Error):
Error.setObjectName("Error")
Error.resize(577, 245)
Error.setModal(True)
self.textBrowser = QtWidgets.QTextBrowser(Error)
self.textBrowser.setGeometry(QtCore.QRect(30, 20, 521, 151))
self.textBrowser.setOverwriteMode(False)
self.textBrowser.setObjectName("textBrowser")
self.pushButton = QtWidgets.QPushButton(Error)
self.pushButton.setGeometry(QtCore.QRect(200, 190, 161, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Error)
self.pushButton.clicked.connect(Error.close)
QtCore.QMetaObject.connectSlotsByName(Error)
def retranslateUi(self, Error):
_translate = QtCore.QCoreApplication.translate
Error.setWindowTitle(_translate("Error", "Error"))
self.textBrowser.setHtml(_translate("Error", "<!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;\"><span style=\" font-size:16pt;\">" + errorMessage + "</span></p></body></html>"))
self.pushButton.setText(_translate("Error", "Close"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Error = QtWidgets.QDialog()
ui = Ui_Error()
ui.setupUi(Error)
Error.show()
sys.exit(app.exec_())
【问题讨论】: