PyQt5之QLineEdit文本框类控件2

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张图片

二、验证器

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张图片

三、输入掩码

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张图片

四、综合示例

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张图片

你可能感兴趣的:(Python)