七月
18
Pyside2教程:使用正则表达式进行输入校验
import sys
from PySide2 import QtWidgets as qtw
from PySide2 import QtGui as qtg
from PySide2 import QtCore as qtc
# 将主程序封装成类
class MainWindow(qtw.QWidget):
def __init__(self):
# 引入父类的__init__方法
super().__init__(parent=None)
# Main UI code goes here
inventory_number = qtw.QLineEdit(self)
entry_validator = qtg.QRegExpValidator(r'^[A-HJ-NP-Z]{2}-\d{3}-\d{4}[A-HJ-NP-Z]$')
inventory_number.setValidator(entry_validator)
# End main UI code
# 显示主窗口
self.show()
if __name__ == '__main__':
app = qtw.QApplication()
main_window = MainWindow()
sys.exit(app.exec_())