import sys from PyQt5.QtWidgets import QApplication,QWidget,QLineEdit,QFormLayout from PyQt5.QtGui import QIntValidator,QDoubleValidator,QRegExpValidator from PyQt5.QtCore import QRegExp class lineEditDemo(QWidget): def __init__(self): super(lineEditDemo, self).__init__() #创建文本输入框 self.lineNormalEcho = QLineEdit() self.lineNoEcho = QLineEdit() self.linePasswd = QLineEdit() self.linePasswdEditEcho = QLineEdit() self.lineReadOnly = QLineEdit() self.lineMaxlen = QLineEdit() self.lineInt = QLineEdit() self.lineFloat = QLineEdit() self.lineRegexp = QLineEdit() self.lineMask = QLineEdit() #设置显示模式 self.lineNormalEcho.setEchoMode(QLineEdit.Normal) self.lineNoEcho.setEchoMode(QLineEdit.NoEcho) self.linePasswd.setEchoMode(QLineEdit.Password) self.linePasswdEditEcho.setEchoMode(QLineEdit.PasswordEchoOnEdit) #设置文本框浮现文字 self.linePasswdEditEcho.setPlaceholderText("linePasswdEditEcho") #在文本框写内容 self.lineReadOnly.setText("this is ReadOnly") #设置文本框只读 self.lineReadOnly.setReadOnly(True) #设置文本长度 self.lineMaxlen.setMaxLength(10) #设置整型验证器 self.intValid = QIntValidator(self) self.intValid.setRange(1,99) #设置文本框输入范围 self.lineInt.setValidator(self.intValid) #设置浮点型验证器 self.floatValid = QDoubleValidator(self) self.floatValid.setRange(-360,360) self.floatValid.setDecimals(4) #小数点位数 self.lineFloat.setValidator(self.floatValid) #设置正则表达式 self.regExpValid = QRegExpValidator(self) # 设置允许输入字符 self.reg = QRegExp('[a-zA-Z0-9]+$') self.regExpValid.setRegExp(self.reg) self.lineRegexp.setValidator(self.regExpValid) #设置掩码 self.lineMask.setInputMask("_*_*_*_*_*_*_*") #设置信号槽 #self.lineNormalEcho.textEdited.connect(self.textEditHandle) #self.lineNormalEcho.textChanged.connect(self.textEditHandle) #self.lineNormalEcho.selectionChanged.connect(self.enterPress) #以'enter'回车键为输入结束 self.lineNormalEcho.editingFinished.connect(self.textEditHandle) #表单布局 gFromlayout = QFormLayout() gFromlayout.addRow("lineNormalEcho",self.lineNormalEcho) gFromlayout.addRow("lineNoEcho",self.lineNoEcho) gFromlayout.addRow("linePasswd",self.linePasswd) gFromlayout.addRow("linePasswdEditEcho",self.linePasswdEditEcho) gFromlayout.addRow("lineReadOnly",self.lineReadOnly) gFromlayout.addRow("lineMaxlen",self.lineMaxlen) gFromlayout.addRow("lineInt",self.lineInt) gFromlayout.addRow("lineFloat",self.lineFloat) gFromlayout.addRow("lineRegexp",self.lineRegexp) gFromlayout.addRow("lineMask",self.lineMask) self.setLayout(gFromlayout) def textEditHandle(self): print(self.lineNormalEcho.text()) def enterPress(self): print("enter:") if __name__ == '__main__': app = QApplication(sys.argv) LineDemo = lineEditDemo() LineDemo.show() sys.exit(app.exec_()) 参考: PyQt5基本控件详解之QLineEdit(四)_jia666666的博客-CSDN博客_pyqt5 qlineedit
PyQt5之QLineEdit(四)
最新推荐文章于 2024-02-21 14:41:10 发布