如何使 pyqt5 多个输入小部件强制输入某些内容?
How to make pyqt5 multiple input widgets mandatory to enter something?
我想在 pyqt5 中强制使用一些输入小部件。如果用户错过了强制输入,我需要给消息。在这种情况下,我想设置强制输入小部件,因为 self.comboComboBox and self.spinSpinBox
绝对是强制性的,并且 self.plainTextEdit and self.doube_one_spinDoubleSpinBox or self.lineLineEdit and self.double_two_spinDoubleSpinBox
是强制性的。
下面是我试过的例子:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(334, 417)
MainWindow.setMinimumSize(QtCore.QSize(334, 417))
MainWindow.setMaximumSize(QtCore.QSize(334, 417))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(20, 10, 291, 351))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.pushButton = QtWidgets.QPushButton(self.frame)
self.pushButton.setGeometry(QtCore.QRect(90, 320, 75, 23))
self.pushButton.setObjectName("pushButton")
self.lineLabel = QtWidgets.QLabel(self.frame)
self.lineLabel.setGeometry(QtCore.QRect(10, 250, 41, 20))
self.lineLabel.setObjectName("lineLabel")
self.lineLineEdit = QtWidgets.QLineEdit(self.frame)
self.lineLineEdit.setGeometry(QtCore.QRect(100, 245, 133, 20))
self.lineLineEdit.setObjectName("lineLineEdit")
self.comboLabel = QtWidgets.QLabel(self.frame)
self.comboLabel.setGeometry(QtCore.QRect(11, 17, 31, 16))
self.comboLabel.setObjectName("comboLabel")
self.comboComboBox = QtWidgets.QComboBox(self.frame)
self.comboComboBox.setGeometry(QtCore.QRect(98, 17, 121, 20))
self.comboComboBox.setObjectName("comboComboBox")
self.spinLabel = QtWidgets.QLabel(self.frame)
self.spinLabel.setGeometry(QtCore.QRect(11, 43, 19, 16))
self.spinLabel.setObjectName("spinLabel")
self.spinSpinBox = QtWidgets.QSpinBox(self.frame)
self.spinSpinBox.setGeometry(QtCore.QRect(98, 43, 121, 20))
self.spinSpinBox.setObjectName("spinSpinBox")
self.doube_one_spinLabel = QtWidgets.QLabel(self.frame)
self.doube_one_spinLabel.setGeometry(QtCore.QRect(13, 180, 79, 21))
self.doube_one_spinLabel.setObjectName("doube_one_spinLabel")
self.doube_one_spinDoubleSpinBox = QtWidgets.QDoubleSpinBox(self.frame)
self.doube_one_spinDoubleSpinBox.setGeometry(QtCore.QRect(100, 180, 131, 20))
self.doube_one_spinDoubleSpinBox.setObjectName("doube_one_spinDoubleSpinBox")
self.double_two_spinLabel = QtWidgets.QLabel(self.frame)
self.double_two_spinLabel.setGeometry(QtCore.QRect(10, 275, 81, 16))
self.double_two_spinLabel.setObjectName("double_two_spinLabel")
self.double_two_spinDoubleSpinBox = QtWidgets.QDoubleSpinBox(self.frame)
self.double_two_spinDoubleSpinBox.setGeometry(QtCore.QRect(100, 275, 131, 20))
self.double_two_spinDoubleSpinBox.setObjectName("double_two_spinDoubleSpinBox")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.frame)
self.plainTextEdit.setGeometry(QtCore.QRect(100, 110, 131, 61))
self.plainTextEdit.setObjectName("plainTextEdit")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 130, 43, 16))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 334, 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)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#
self.pushButton.clicked.connect(self.okay)
self.comboComboBox.addItems(["", "One", "Two"])
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Okay"))
self.lineLabel.setText(_translate("MainWindow", "line"))
self.comboLabel.setText(_translate("MainWindow", "combo"))
self.spinLabel.setText(_translate("MainWindow", "spin"))
self.doube_one_spinLabel.setText(_translate("MainWindow", "doube_one_spin"))
self.double_two_spinLabel.setText(_translate("MainWindow", "double_two_spin"))
self.label.setText(_translate("MainWindow", "Plain Edit"))
def okay(self):
if self.comboComboBox.currentText() !='' and self.spinSpinBox.value() !=0 and\
(self.plainTextEdit.toPlainText() !='' and self.doube_one_spinDoubleSpinBox.value() !=0.00) or\
(self.lineLineEdit.text() !='' and self.double_two_spinDoubleSpinBox.value() != 0.00):
QtWidgets.QMessageBox.about(self, "Information", "Done.")
else:
QtWidgets.QMessageBox.about(self, "Information", "Required Fields are Mandatory.")
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_())
在我的示例中,如果我在 self.lineLineEdit 和 self.double_two_spinDoubleSpinBox 中填充了内容,即使 self.comboComboBox 和 self.spinSpinBox 为空,它也会打印“OKAY”。
如何解决这个问题?或者有什么办法吗?如果它简单易行,它将非常有用。怎么办?
Update:
我的要求是我有 6 个小部件。 pair1(self.comboComboBox, self.spinSpinBox), pair2(self.plainTextEdit, self.doube_one_spinDoubleSpinBox), pair3(self.lineLineEdit, self.double_two_spinDoubleSpinBox)
。我想设置强制字段,因为 pair1 是强制的。并且 (either pair2 or pair 3
) 与 pair1 一起是强制性的。
根据评论,我得到了缺失的点,并重新组合了组合。现在它工作如下。
if self.comboComboBox.currentText() !='' and self.spinSpinBox.value() !=0 and\
((self.plainTextEdit.toPlainText() !='' and self.doube_one_spinDoubleSpinBox.value() !=0.00) or\
(self.lineLineEdit.text() !='' and self.double_two_spinDoubleSpinBox.value() != 0.00)):
QtWidgets.QMessageBox.about(self, "Information", "Done.")
else:
QtWidgets.QMessageBox.about(self, "Information", "Required Fields are Mandatory.")
我想在 pyqt5 中强制使用一些输入小部件。如果用户错过了强制输入,我需要给消息。在这种情况下,我想设置强制输入小部件,因为 self.comboComboBox and self.spinSpinBox
绝对是强制性的,并且 self.plainTextEdit and self.doube_one_spinDoubleSpinBox or self.lineLineEdit and self.double_two_spinDoubleSpinBox
是强制性的。
下面是我试过的例子:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(334, 417)
MainWindow.setMinimumSize(QtCore.QSize(334, 417))
MainWindow.setMaximumSize(QtCore.QSize(334, 417))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(20, 10, 291, 351))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.pushButton = QtWidgets.QPushButton(self.frame)
self.pushButton.setGeometry(QtCore.QRect(90, 320, 75, 23))
self.pushButton.setObjectName("pushButton")
self.lineLabel = QtWidgets.QLabel(self.frame)
self.lineLabel.setGeometry(QtCore.QRect(10, 250, 41, 20))
self.lineLabel.setObjectName("lineLabel")
self.lineLineEdit = QtWidgets.QLineEdit(self.frame)
self.lineLineEdit.setGeometry(QtCore.QRect(100, 245, 133, 20))
self.lineLineEdit.setObjectName("lineLineEdit")
self.comboLabel = QtWidgets.QLabel(self.frame)
self.comboLabel.setGeometry(QtCore.QRect(11, 17, 31, 16))
self.comboLabel.setObjectName("comboLabel")
self.comboComboBox = QtWidgets.QComboBox(self.frame)
self.comboComboBox.setGeometry(QtCore.QRect(98, 17, 121, 20))
self.comboComboBox.setObjectName("comboComboBox")
self.spinLabel = QtWidgets.QLabel(self.frame)
self.spinLabel.setGeometry(QtCore.QRect(11, 43, 19, 16))
self.spinLabel.setObjectName("spinLabel")
self.spinSpinBox = QtWidgets.QSpinBox(self.frame)
self.spinSpinBox.setGeometry(QtCore.QRect(98, 43, 121, 20))
self.spinSpinBox.setObjectName("spinSpinBox")
self.doube_one_spinLabel = QtWidgets.QLabel(self.frame)
self.doube_one_spinLabel.setGeometry(QtCore.QRect(13, 180, 79, 21))
self.doube_one_spinLabel.setObjectName("doube_one_spinLabel")
self.doube_one_spinDoubleSpinBox = QtWidgets.QDoubleSpinBox(self.frame)
self.doube_one_spinDoubleSpinBox.setGeometry(QtCore.QRect(100, 180, 131, 20))
self.doube_one_spinDoubleSpinBox.setObjectName("doube_one_spinDoubleSpinBox")
self.double_two_spinLabel = QtWidgets.QLabel(self.frame)
self.double_two_spinLabel.setGeometry(QtCore.QRect(10, 275, 81, 16))
self.double_two_spinLabel.setObjectName("double_two_spinLabel")
self.double_two_spinDoubleSpinBox = QtWidgets.QDoubleSpinBox(self.frame)
self.double_two_spinDoubleSpinBox.setGeometry(QtCore.QRect(100, 275, 131, 20))
self.double_two_spinDoubleSpinBox.setObjectName("double_two_spinDoubleSpinBox")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.frame)
self.plainTextEdit.setGeometry(QtCore.QRect(100, 110, 131, 61))
self.plainTextEdit.setObjectName("plainTextEdit")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 130, 43, 16))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 334, 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)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#
self.pushButton.clicked.connect(self.okay)
self.comboComboBox.addItems(["", "One", "Two"])
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Okay"))
self.lineLabel.setText(_translate("MainWindow", "line"))
self.comboLabel.setText(_translate("MainWindow", "combo"))
self.spinLabel.setText(_translate("MainWindow", "spin"))
self.doube_one_spinLabel.setText(_translate("MainWindow", "doube_one_spin"))
self.double_two_spinLabel.setText(_translate("MainWindow", "double_two_spin"))
self.label.setText(_translate("MainWindow", "Plain Edit"))
def okay(self):
if self.comboComboBox.currentText() !='' and self.spinSpinBox.value() !=0 and\
(self.plainTextEdit.toPlainText() !='' and self.doube_one_spinDoubleSpinBox.value() !=0.00) or\
(self.lineLineEdit.text() !='' and self.double_two_spinDoubleSpinBox.value() != 0.00):
QtWidgets.QMessageBox.about(self, "Information", "Done.")
else:
QtWidgets.QMessageBox.about(self, "Information", "Required Fields are Mandatory.")
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_())
在我的示例中,如果我在 self.lineLineEdit 和 self.double_two_spinDoubleSpinBox 中填充了内容,即使 self.comboComboBox 和 self.spinSpinBox 为空,它也会打印“OKAY”。 如何解决这个问题?或者有什么办法吗?如果它简单易行,它将非常有用。怎么办?
Update:
我的要求是我有 6 个小部件。 pair1(self.comboComboBox, self.spinSpinBox), pair2(self.plainTextEdit, self.doube_one_spinDoubleSpinBox), pair3(self.lineLineEdit, self.double_two_spinDoubleSpinBox)
。我想设置强制字段,因为 pair1 是强制的。并且 (either pair2 or pair 3
) 与 pair1 一起是强制性的。
根据评论,我得到了缺失的点,并重新组合了组合。现在它工作如下。
if self.comboComboBox.currentText() !='' and self.spinSpinBox.value() !=0 and\
((self.plainTextEdit.toPlainText() !='' and self.doube_one_spinDoubleSpinBox.value() !=0.00) or\
(self.lineLineEdit.text() !='' and self.double_two_spinDoubleSpinBox.value() != 0.00)):
QtWidgets.QMessageBox.about(self, "Information", "Done.")
else:
QtWidgets.QMessageBox.about(self, "Information", "Required Fields are Mandatory.")