How to restrict user input in QLineEdit in pyqt

↘锁芯ラ 提交于 2019-12-03 06:19:02

you can use QValidator it works like:

#To allow only int
self.onlyInt = QIntValidator()
self.LineEdit.setValidator(self.onlyInt)

you can use exception handling for validating this:

number = self.ui.number_lineEdit.text()
try:
    number = int(number)
except Exception:
    QtGui.QMessageBox.about(self, 'Error','Input can only be a number')
    pass

you can also use validators to validate input strings.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!