简体   繁体   English

如何使 pyqt5 多个输入小部件强制输入内容?

[英]How to make pyqt5 multiple input widgets mandatory to enter something?

I want to make some input widgets mandatory in pyqt5.我想在 pyqt5 中强制使用一些输入小部件。 I need to give message If user missed the mandatory inputs.如果用户错过了强制输入,我需要给出消息。 In this scenario i want to set mandatory input widgets as self.comboComboBox and self.spinSpinBox are definitely mandatory and either self.plainTextEdit and self.doube_one_spinDoubleSpinBox or self.lineLineEdit and self.double_two_spinDoubleSpinBox are mandatory.在这种情况下,我想将强制性输入小部件设置为self.comboComboBox and self.spinSpinBox绝对是强制性的,并且self.plainTextEdit and self.doube_one_spinDoubleSpinBox or self.lineLineEdit and self.double_two_spinDoubleSpinBox是强制性的。 Below is my tried example:下面是我尝试过的例子:

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_())

In my example it is printing "OKAY" if i filled something in self.lineLineEdit and self.double_two_spinDoubleSpinBox even self.comboComboBox and self.spinSpinBox are empty.在我的示例中,如果我在 self.lineLineEdit 和 self.double_two_spinDoubleSpinBox 中填写了一些内容,即使 self.comboComboBox 和 self.spinSpinBox 为空,它也会打印“OKAY”。 How to solve this?如何解决这个问题? or Is there any way?或者有什么办法吗? Its very useful if it is simple and easy.如果它简单易行,它非常有用。 How to do?怎么做?

Update:

My requirement is that i have 6 widgets.我的要求是我有 6 个小部件。 pair1(self.comboComboBox, self.spinSpinBox), pair2(self.plainTextEdit, self.doube_one_spinDoubleSpinBox), pair3(self.lineLineEdit, self.double_two_spinDoubleSpinBox) . pair1(self.comboComboBox, self.spinSpinBox), pair2(self.plainTextEdit, self.doube_one_spinDoubleSpinBox), pair3(self.lineLineEdit, self.double_two_spinDoubleSpinBox) I want to set mandatory feilds as pair1 is mandatory.我想设置强制性字段,因为 pair1 是强制性的。 And ( either pair2 or pair 3 ) is mandatory along with pair1.并且( either pair2 or pair 3 )与 pair1 一起是强制性的。

As per the comments, I Got the the missing points and I regrouped the combinations.根据评论,我得到了缺失的点并重新组合了组合。 Now It is working as below.现在它的工作如下。

        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.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM