欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PyQt5基本窗口控件-----QLineEdit

程序员文章站 2022-05-28 11:07:39
...

PyQt5学习之路,点击有pyqt5学习笔记

QLineEdit类是一个单行文本控件,可输入单行字符串,可以设置回显模式(Echomode)和掩码模式

1. 回显模式(Echomode)
回显模式就是当键盘被按下后,显示了什么

  • Normal 正常的回显模式
  • NoEcho 不回显模式(什么都没出现)
  • Password 密码
  • PasswordEchoOnEdit 先是显示,然后过了几秒就不显示
import sys
from PyQt5.QtWidgets import *

class QLineEditEchoMode(QWidget):
    def __init__(self):
        super(QLineEditEchoMode, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('文本框的回显模式')

        formLayout = QFormLayout()

        normalLineEdit = QLineEdit()
        noEchoLineEdit = QLineEdit()
        passwordLineEdit = QLineEdit()
        passwordEchoOnLineEdit = QLineEdit()

        formLayout.addRow("Normal", normalLineEdit)
        formLayout.addRow("noEcho", noEchoLineEdit)
        formLayout.addRow("password", passwordLineEdit)
        formLayout.addRow("passwordEchoOnEdit", passwordEchoOnLineEdit)

        # placeoldertext  文本输入框内的灰色提示
        normalLineEdit.setPlaceholderText("Normal")
        noEchoLineEdit.setPlaceholderText("noEcho")
        passwordLineEdit.setPlaceholderText("password")
        passwordEchoOnLineEdit.setPlaceholderText("passwordEchoOnEdit")

        normalLineEdit.setEchoMode(QLineEdit.Normal)
        noEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
        passwordLineEdit.setEchoMode(QLineEdit.Password)
        passwordEchoOnLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)

        self.setLayout(formLayout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QLineEditEchoMode()
    main.show()
    sys.exit(app.exec_())

PyQt5基本窗口控件-----QLineEdit

2. 校验器
比如只能限制输入整数或满足一定条件的字符串

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIntValidator, QDoubleValidator, QRegExpValidator
from PyQt5.QtCore import QRegExp

class QLineEditValiddator(QWidget):
    def __init__(self):
        super(QLineEditValiddator, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('校验器')

        # 创建表单布局
        formLayout = QFormLayout()

        intLineEdit = QLineEdit()
        DoubleLineEdit = QLineEdit()
        validatorLineEdit = QLineEdit()

        formLayout.addRow('整数类型', intLineEdit)
        formLayout.addRow('浮点类型', DoubleLineEdit)
        formLayout.addRow('数字和字母类型', validatorLineEdit)

        intLineEdit.setPlaceholderText('整形')
        DoubleLineEdit.setPlaceholderText('浮点型')
        validatorLineEdit.setPlaceholderText('字母和数字')

        # 整数校验器
        # 创建一个校验器
        intValidator = QIntValidator(self)
        # 设置范围
        intValidator.setRange(1, 99)

        # 浮点校验器
        # 创建一个校验器
        doubleValidator = QDoubleValidator(self)
        # 设置范围
        doubleValidator.setRange(-360, 360)
        # 设置精度
        doubleValidator.setNotation(QDoubleValidator.StandardNotation)
        doubleValidator.setDecimals(2)

        # 字符和数字校验器
        reg = QRegExp('[a-zA-Z0-9]+$')
        # 创建一个校验器
        validator = QRegExpValidator(self)
        validator.setRegExp(reg)

        # 设置校验器
        intLineEdit.setValidator(intValidator)
        DoubleLineEdit.setValidator(doubleValidator)
        validatorLineEdit.setValidator(validator)

        self.setLayout(formLayout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QLineEditValiddator()
    main.show()
    sys.exit(app.exec_())

PyQt5基本窗口控件-----QLineEdit

3. 掩码
要限制用户的输入,除了刚刚的校验器外,还有掩码,比如在修改IP时,这个就是掩码
PyQt5基本窗口控件-----QLineEdit

A    ASCII字母字符是必须输入的(A-Z、a-z)
a    ASCII字母字符是允许输入的,但不是必需的(A-Z、a-z)
N    ASCII字母字符是必须输入的(A-Z、a-z、0-9)
n    ASII字母字符是允许输入的,但不是必需的(A-Z、a-z、0-9)
X    任何字符都是必须输入的
x    任何字符都是允许输入的,但不是必需的
9    ASCII数字字符是必须输入的(0-9)
0    ASCII数字字符是允许输入的,但不是必需的(0-9)
D    ASCII数字字符是必须输入的(1-9)
d    ASCII数字字符是允许输入的,但不是必需的(1-9)
#    ASCI数字字符或加减符号是允许输入的,但不是必需的
H    十六进制格式字符是必须输入的(A-F、a-f、0-9)
h    十六进制格式字符是允许输入的,但不是必需的(A-F、a-f、0-9)
B    二进制格式字符是必须输入的(0,1)
b    二进制格式字符是允许输入的,但不是必需的(0,1)
>    所有的字母字符都大写
<    所有的字母字符都小写
!    关闭大小写转换
\    使用"\"转义上面列出的字符
import sys
from PyQt5.QtWidgets import *

class QLineEditMask(QWidget):
    def __init__(self):
        super(QLineEditMask, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('用掩码限制QLineEdit控件的输入')
        formlayout = QFormLayout()

        ipLineEdit = QLineEdit()
        macLineEdit = QLineEdit()
        dateLineEdit = QLineEdit()
        licenseLineEdit = QLineEdit()

        ipLineEdit.setInputMask('000.000.000.000;_')  # 没有输入时就显示成下划线
        macLineEdit.setInputMask('HH:HH:HH:HH:HH:HH;_')
        dateLineEdit.setInputMask('0000-00-00')
        licenseLineEdit.setInputMask('>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#')

        formlayout.addRow('数字掩码', ipLineEdit)
        formlayout.addRow('Mac掩码', macLineEdit)
        formlayout.addRow('日期掩码', dateLineEdit)
        formlayout.addRow('许可证掩码', licenseLineEdit)

        self.setLayout(formlayout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QLineEditMask()
    main.show()
    sys.exit(app.exec_())


PyQt5基本窗口控件-----QLineEdit

4. Demo

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt

class QLineEditDemo(QWidget):
    def __init__(self):
        super(QLineEditDemo, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('DEMO')

        edit1 = QLineEdit()
        # 使用int校验器
        edit1.setValidator(QIntValidator())
        # 设置最大长度,不超过4位的int
        edit1.setMaxLength(4)
        # 设置文本对齐格式
        edit1.setAlignment(Qt.AlignRight)
        # 设置字体和大小
        edit1.setFont(QFont('Arial', 20))

        # 设置浮点数校验
        edit2 = QLineEdit()
        edit2.setValidator(QDoubleValidator(0.99, 99.99, 2))

        # 设置掩码
        edit3 = QLineEdit()
        edit3.setInputMask('99_9999_999999;#')

        # 绑定信号槽,显示文本变化
        edit4 = QLineEdit()
        edit4.textChanged.connect(self.textChanged)

        # 设置回显password
        edit5 = QLineEdit()
        edit5.setEchoMode(QLineEdit.Password)
        edit5.editingFinished.connect(self.enterPress)

        # 设为只读
        edit6 = QLineEdit("hello pyqt5")
        edit6.setReadOnly(True)

        formlayout = QFormLayout()
        formlayout.addRow('整形校验', edit1)
        formlayout.addRow('浮点数校验', edit2)
        formlayout.addRow('Input Mask校验', edit3)
        formlayout.addRow('文本变化', edit4)
        formlayout.addRow('密码', edit5)
        formlayout.addRow('只读', edit6)

        self.setLayout(formlayout)

    def textChanged(self, text):
        print("输出的内容" + text)

    def enterPress(self):
        print("已输入值")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QLineEditDemo()
    main.show()
    sys.exit(app.exec_())

PyQt5基本窗口控件-----QLineEdit