【发布时间】:2020-08-18 16:42:56
【问题描述】:
我的QRegExpValidator 总是认为我的输入是Acceptable - 它不应该是。在 gui.py 中有一个名为 `
QLineEdit 对象
from gui import Ui_Dialog
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5 import QtCore
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtCore import QRegExp
import sys
class AppWindow(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.show()
self.ui.number.textChanged[str].connect(self.validate_number)
def validate_number(self):
regex = QRegExp("^[0-9]{3}$")
tmp = QRegExpValidator(regex, self.ui.number)
print(tmp.Acceptable)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())
无论我尝试什么,输出总是2。我希望我输入任何内容,如果它是一个 3 位数字,则该函数返回 True(或 Acceptable)。
我试图按照这条路径解释here,我错过了什么?
【问题讨论】:
标签: python pyqt pyqt5 qlineedit qvalidator