from PyQt5.Qt import *
import sys
import math
#设置验证器
class AgeVadidator(QValidator):
def validate(self, input_str, pos_int):
print(input_str,pos_int)
#判定
#结果字符串,应该全部都是由一些数字组成
#return
try:
if 18<=int(input_str)<=180:
return (QValidator.Acceptable,input_str,pos_int)#验证通过
elif 1<=int(input_str)<=17:
return (QValidator.Intermediate,input_str,pos_int)#暂进被允许
else:
return (QValidator.Invalid,input_str,pos_int)#验证不通过
except:
if len(input_str)==0:
return (QValidator.Intermediate,input_str,pos_int)
return (QValidator.Invalid,input_str,pos_int)
def fixup(self,p_str):
print("xxx",p_str)
try:
if int(p_str)<18:
return "18"
return "180"
except:
return "18"
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QLineEdit验证器的学习")
self.resize(500,500)
self.setup_ui()
def setup_ui(self):
le=QLineEdit(self)
le.move(100,100)
#18-180
vadidator=AgeVadidator()
le.setValidator(vadidator)
le1=QLineEdit(self)
le1.move(200,200)
App=QApplication(sys.argv)
Win=Window()
Win.show()
sys.exit(App.exec_())