我如何验证并退出QInputDialog?

时间:2014-03-11 12:18:37

标签: python validation user-interface input pyqt4

...
Textbox = QtGui.QInputDialog.getText(self, 'Get Name', 
            'Enter  Name:')
        Text =Texbox[0]
Textbox2 = QtGui.QInputDialog.getText(self, 'Get Surname', 
            'Enter  Surname:')
        Text2 =Texbox2[0]
...

我正在使用python 3.3和pyqt4。在制作GUI时,我要求用户使用函数中的多个弹出对话框输入特定的详细信息,但是当没有输入任何内容时,它将接受这样的值,而不是要求用户重新输入。还可以选择关闭对话框(右上角的退出按钮),但是单击此按钮时,将运行下一个对话框,而不是停止整个功能。 我希望激活退出按钮时对话框停止运行,并且对话框不允许输入任何值。如果需要任何其他信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

使用表单收集此信息。您可以在下面看到如下代码的对话框。这段代码可能并不完全符合您的需要,但确保您指明正确的方向。

enter image description here

这是代码(使用QtDesigner生成pyuic4

注意: 自动生成的国际化内容已被移除,以使示例更加简洁。

from PyQt4 import QtCore, QtGui

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(371, 113)
        self.gridLayout_2 = QtGui.QGridLayout(Dialog)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.label = QtGui.QLabel(Dialog)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.lineEdit = QtGui.QLineEdit(Dialog)
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
        self.lineEdit_2 = QtGui.QLineEdit(Dialog)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.gridLayout.addWidget(self.lineEdit_2, 1, 1, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.gridLayout_2.addWidget(self.buttonBox, 1, 0, 1, 1)

        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())