1 import sys 2 from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QFormLayout 3 from PyQt5.QtGui import QIntValidator, QDoubleValidator, QFont 4 from PyQt5.QtCore import Qt 5 6 7 class lineEditDemo(QWidget): 8 9 def __init__(self): 10 super(lineEditDemo, self).__init__() 11 12 e1 = QLineEdit() 13 e1.setValidator(QIntValidator()) 14 e1.setMaxLength(4) 15 e1.setAlignment(Qt.AlignRight) 16 e1.setFont(QFont("Arial", 20)) 17 e2 = QLineEdit() 18 e2.setValidator(QDoubleValidator(0.99, 99.99, 2)) 19 flo = QFormLayout() 20 flo.addRow("integer validator", e1) 21 flo.addRow("Double validateor", e2) 22 e3 = QLineEdit() 23 e3.setInputMask("+99_9999_999999") 24 flo.addRow("Input Mask", e3) 25 e4 = QLineEdit() 26 e4.textChanged.connect(self.textchanged) 27 flo.addRow("Text changed", e4) 28 e5 = QLineEdit() 29 e5.setEchoMode(QLineEdit.Password) 30 flo.addRow("Password", e5) 31 e6 = QLineEdit("Hello PyQt5") 32 e6.setReadOnly(True) 33 flo.addRow("Read Only", e6) 34 e5.editingFinished.connect(self.enterPress) 35 self.setLayout(flo) 36 self.setWindowTitle("QLineEdit例子") 37 38 def textchanged(self, text): 39 print("输入的内容为: "+ text) 40 41 def enterPress(self): 42 print("已输入值") 43 44 if __name__ == '__main__': 45 app = QApplication(sys.argv) 46 win = lineEditDemo() 47 win.show() 48 sys.exit(app.exec_())
效果图: