PyQt5之QLineEdit文本框类控件2
一、EchoMode的显示效果
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
class Win(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 400, 400)
self.setWindowTitle('QLineEdit演示')
layout = QFormLayout()
pNormal = QLineEdit()
pNoEcho = QLineEdit()
pPassword = QLineEdit()
pPasswordEchoOnEdit = QLineEdit()
layout.addRow("Normal",pNormal)
layout.addRow("NoEcho",pNoEcho)
layout.addRow("Password",pPassword)
layout.addRow("PasswordEchoOnEdit",pPasswordEchoOnEdit)
self.setLayout(layout)
pNormal.setPlaceholderText("Normal")
pNoEcho.setPlaceholderText("NoEcho")
pPassword.setPlaceholderText("Password")
pPasswordEchoOnEdit.setPlaceholderText("PasswordEchoOnEdit")
pNormal.setEchoMode(QLineEdit.Normal)
pNoEcho.setEchoMode(QLineEdit.NoEcho)
pPassword.setEchoMode(QLineEdit.Password)
pPasswordEchoOnEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Win()
form.show()
sys.exit(app.exec_())
运行效果如下:
![PyQt5之QLineEdit文本框类控件2_第1张图片](http://img.e-com-net.com/image/info8/c57a724548154fd4aff5190f6015f637.png)
二、验证器
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import QRegExp
class Win(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 400, 400)
self.setWindowTitle('QLineEdit演示')
layout = QFormLayout()
pInt = QLineEdit()
pDouble = QLineEdit()
pValidator = QLineEdit()
layout.addRow("整型",pInt)
layout.addRow("浮点型",pDouble)
layout.addRow("字母和数字",pValidator)
self.setLayout(layout)
pInt.setPlaceholderText("整型")
pDouble.setPlaceholderText("浮点型")
pValidator.setPlaceholderText("字母和数字")
pIntVali = QIntValidator(self)
pIntVali.setRange(1,99)
pDoubleVali = QDoubleValidator(self)
pDoubleVali.setRange(-360,360)
pDoubleVali.setNotation(QDoubleValidator.StandardNotation)
pDoubleVali.setDecimals(2)
reg = QRegExp("[a-zA-Z0-9]+$")
pVali = QRegExpValidator(self)
pVali.setRegExp(reg)
pInt.setValidator(pIntVali)
pDouble.setValidator(pDoubleVali)
pValidator.setValidator(pVali)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Win()
form.show()
sys.exit(app.exec_())
运行效果如下:
![PyQt5之QLineEdit文本框类控件2_第2张图片](http://img.e-com-net.com/image/info8/7b4b0c9f98e74ec0a94e82632ad0d9f5.png)
三、输入掩码
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import QRegExp
class Win(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 400, 400)
self.setWindowTitle('QLineEdit演示')
layout = QFormLayout()
pIP = QLineEdit()
pMAC = QLineEdit()
pDate = QLineEdit()
pLicense = QLineEdit()
pIP.setInputMask("000.000.000.000;_")
pMAC.setInputMask("HH:HH:HH:HH:HH:HH;_")
pDate.setInputMask("0000-00-00")
pLicense.setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#")
layout.addRow("数字掩码",pIP)
layout.addRow("MAC掩码",pMAC)
layout.addRow("日期掩码",pDate)
layout.addRow("许可证掩码", pLicense)
self.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Win()
form.show()
sys.exit(app.exec_())
运行效果如下:
![PyQt5之QLineEdit文本框类控件2_第3张图片](http://img.e-com-net.com/image/info8/36c31f0cc3694f5799ab6b1a710437b4.png)
四、综合示例
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import QRegExp,Qt
class Win(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 400, 400)
self.setWindowTitle('综合示例')
layout = QFormLayout()
e1 = QLineEdit()
e1.setValidator(QIntValidator())
e1.setMaxLength(4)
e1.setAlignment(Qt.AlignRight)
e1.setFont(QFont("Arial",20))
layout.addRow("Integer Validator",e1)
e2 = QLineEdit()
e2.setValidator(QDoubleValidator(0.99,99.99,2))
layout.addRow("Double Validator",e2)
e3 = QLineEdit()
e3.setInputMask('+99_9999_999999')
layout.addRow("Input Mask",e3)
e4 = QLineEdit()
e4.textChanged.connect(self.textchanged)
layout.addRow("Text Changed",e4)
e5 = QLineEdit()
e5.setEchoMode(QLineEdit.Password)
e5.editingFinished.connect(self.enterPress)
layout.addRow("Password",e5)
e6 = QLineEdit("Hello PyQt5")
e6.setReadOnly(True)
layout.addRow("Read Only",e6)
self.setLayout(layout)
def textchanged(self,text):
print("输入的内容为:"+text)
def enterPress(self):
print("已输入值")
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Win()
form.show()
sys.exit(app.exec_())
运行效果如下:
![PyQt5之QLineEdit文本框类控件2_第4张图片](http://img.e-com-net.com/image/info8/2e4db961abf847189dcd253e910cd8ff.png)