使LineEdit仅接受整数

时间:2019-05-20 22:48:58

标签: python pyqt pyqt5

这很简单,我希望我的lineEdit1和lineEdit2接受int输入,而lineEdit3则显示lineEdit1 + lineEdit2。我认为这将是一件容易的事,但是我错了:D。无论如何,我学会了一个QintValidator(是的,我实际上是在研究这个问题),但是经过数小时的搜索,寻找了如何在我的代码中实际实现这一点的答案之后,我放弃了,决定在提出这个问题之前在这里询问作为重复,我向您保证它不是重复线程,其余所有线程都已经使用了几年,或者它们的解释不充分,或者答案是针对pyqt4的,我需要针对pyqt5的答案。

我尝试了很多组合以使qintvalidator正常工作,但它从未成功。我真的不知道如何实现它。

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import  QSize
import sys


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.setFixedSize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(140, 200, 91, 30))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(30, 40, 113, 30))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(Form)
        self.lineEdit_2.setGeometry(QtCore.QRect(210, 40, 113, 30))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_3 = QtWidgets.QLineEdit(Form)
        self.lineEdit_3.setGeometry(QtCore.QRect(120, 130, 113, 30))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)


    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))

        self.pushButton.clicked.connect(self.klik)

    def klik(self):
        textbox1 = self.lineEdit.text()
        textbox2 = self.lineEdit_2.text()
        self.lineEdit_3.setText(textbox1 + textbox2)



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())'''

0 个答案:

没有答案