使用QTDesigner在版本5中,我已经完成了一个主窗口,现在我想在主窗口中单击按钮创建一个QMessagebox。
我得到一个错误:参数1有意外类型'Ui_MainWindow'
我不知道为什么这是不工作,希望你能帮助我,我真的是新的编程:
我添加的行:
self.pushButton.clicked.connect(QtWidgets.QMessageBox.information(self, "Empty String", "Please enter day."))
:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(320, 240)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(210, 160, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 320, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(QtWidgets.QMessageBox.information(self, "Empty String", "Please enter day."))
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
每行前的正确空格(称为缩进)在Python中是至关重要的。在添加的行之前输入8个空格。
在你的代码中替换
self.pushButton.clicked.connect(QtWidgets.QMessageBox.information(self, "Empty String", "Please enter day."))
self.pushButton.clicked.connect(
QtWidgets.QMessageBox.information(
self.centralwidget, "Empty String", "Please enter day."
)
)
注意,在ui中声明了self.centralwidget
,并且类型为QWidget
。