PyQt5 系统化学习: 文本框类控件

《PyQt5快速开发与实战》学习笔记。

6 文本框类控件

QLineEdit 类是一个单行文本框控件,可以输入单行字符串。如果需要输入多行字符串,则使用 QTextEdit 类。

6.1 QLineEdit

QLineEdit 类中的常用方法:

定义输入掩码的字符,并说明其如何控制数据输入:

掩码由掩码字符和分隔符字符串组成,后面可以跟一个分号和空白字符,空白字符在编辑后会从文本中删除的。掩码示例如下所示:

QLineEdit 类中的常用信号:

下面创建一个简单的表单:

from PyQt5.QtWidgets import QApplication,  QLineEdit, QWidget,  QFormLayout
import sys


class lineEditDemo(QWidget):
    def __init__(self, parent=None):
        super(lineEditDemo, self).__init__(parent)
        self.setWindowTitle("QLineEdit例子")

        flo = QFormLayout()
        pNormalLineEdit = QLineEdit()
        pNoEchoLineEdit = QLineEdit()
        pPasswordLineEdit = QLineEdit()
        pPasswordEchoOnEditLineEdit = QLineEdit()

        flo.addRow("Normal", pNormalLineEdit)
        flo.addRow("NoEcho", pNoEchoLineEdit)
        flo.addRow("Password", pPasswordLineEdit)
        flo.addRow("PasswordEchoOnEdit", pPasswordEchoOnEditLineEdit)

        pNormalLineEdit.setPlaceholderText("Normal")
        pNoEchoLineEdit.setPlaceholderText("NoEcho")
        pPasswordLineEdit.setPlaceholderText("Password")
        pPasswordEchoOnEditLineEdit.setPlaceholderText("PasswordEchoOnEdit")

        # 设置显示效果
        pNormalLineEdit.setEchoMode(QLineEdit.Normal)
        pNoEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
        pPasswordLineEdit.setEchoMode(QLineEdit.Password)
        pPasswordEchoOnEditLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)

        self.setLayout(flo)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = lineEditDemo()
    win.show()
    sys.exit(app.exec_())

效果如下:

图1 创建表单

在通常情况下,我们会对用户的输入做一些限制,可以通过验证器来进行。常见的验证器有:整型验证器、浮点型验证器及其他自定义验证器。

from PyQt5.QtWidgets import QApplication,  QLineEdit, QWidget,  QFormLayout
from PyQt5.QtGui import QIntValidator, QDoubleValidator, QRegExpValidator
from PyQt5.QtCore import QRegExp
import sys


class lineEditDemo(QWidget):
    def __init__(self, parent=None):
        super(lineEditDemo, self).__init__(parent)
        self.setWindowTitle("QLineEdit例子")

        flo = QFormLayout()
        pIntLineEdit = QLineEdit()
        pDoubleLineEdit = QLineEdit()
        pValidatorLineEdit = QLineEdit()

        flo.addRow("整形", pIntLineEdit)
        flo.addRow("浮点型", pDoubleLineEdit)
        flo.addRow("字母和数字", pValidatorLineEdit)

        pIntLineEdit.setPlaceholderText("整形")
        pDoubleLineEdit.setPlaceholderText("浮点型")
        pValidatorLineEdit.setPlaceholderText("字母和数字")

        # 整形 范围:[1, 99]
        pIntValidator = QIntValidator(self)
        pIntValidator.setRange(1, 99)

        # 浮点型 范围:[-360, 360] 精度:小数点后2位
        pDoubleValidator = QDoubleValidator(self)
        pDoubleValidator.setRange(-360, 360)
        pDoubleValidator.setNotation(QDoubleValidator.StandardNotation)
        pDoubleValidator.setDecimals(2)

        # 字符和数字
        reg = QRegExp("[a-zA-Z0-9]+$")
        pValidator = QRegExpValidator(self)
        pValidator.setRegExp(reg)

        # 设置验证器
        pIntLineEdit.setValidator(pIntValidator)
        pDoubleLineEdit.setValidator(pDoubleValidator)
        pValidatorLineEdit.setValidator(pValidator)

        self.setLayout(flo)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = lineEditDemo()
    win.show()
    sys.exit(app.exec_())

效果:

图2 有验证功能的表单

要限制用户的输入,除了使用验证器,还可以使用输入掩码,常见的有 IP 地址、MAC 地址、日期、许可证号等。

from PyQt5.QtWidgets import QApplication,  QLineEdit, QWidget,  QFormLayout
import sys


class lineEditDemo(QWidget):
    def __init__(self, parent=None):
        super(lineEditDemo, self).__init__(parent)
        self.setWindowTitle("QLineEdit的输入掩码例子")

        flo = QFormLayout()
        pIPLineEdit = QLineEdit()
        pMACLineEdit = QLineEdit()
        pDateLineEdit = QLineEdit()
        pLicenseLineEdit = QLineEdit()

        pIPLineEdit.setInputMask("000.000.000.000;_")
        pMACLineEdit.setInputMask("HH:HH:HH:HH:HH:HH;_")
        pDateLineEdit.setInputMask("0000-00-00")
        pLicenseLineEdit.setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#")

        flo.addRow("数字掩码", pIPLineEdit)
        flo.addRow("Mac掩码", pMACLineEdit)
        flo.addRow("日期掩码", pDateLineEdit)
        flo.addRow("许可证掩码", pLicenseLineEdit)

        #pIPLineEdit.setPlaceholderText("111")
        #pMACLineEdit.setPlaceholderText("222")
        #pLicenseLineEdit.setPlaceholderText("333")
        #pLicenseLineEdit.setPlaceholderText("444")

        self.setLayout(flo)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = lineEditDemo()
    win.show()
    sys.exit(app.exec_())

效果:

图2 掩码表单

更复杂的例子:

from PyQt5.QtWidgets import QApplication,  QLineEdit, QWidget,  QFormLayout
from PyQt5.QtGui import QIntValidator, QDoubleValidator, QFont
from PyQt5.QtCore import Qt
import sys


class lineEditDemo(QWidget):
    def __init__(self, parent=None):
        super(lineEditDemo, self).__init__(parent)
        e1 = QLineEdit()
        e1.setValidator(QIntValidator())
        e1.setMaxLength(4)
        e1.setAlignment(Qt.AlignRight)
        e1.setFont(QFont("Arial", 20))
        e2 = QLineEdit()
        e2.setValidator(QDoubleValidator(0.99, 99.99, 2))
        flo = QFormLayout()
        flo.addRow("integer validator", e1)
        flo.addRow("Double validator", e2)
        e3 = QLineEdit()
        e3.setInputMask('+99_9999_999999')
        flo.addRow("Input Mask", e3)
        e4 = QLineEdit()
        e4.textChanged.connect(self.textchanged)
        flo.addRow("Text changed", e4)
        e5 = QLineEdit()
        e5.setEchoMode(QLineEdit.Password)
        flo.addRow("Password", e5)
        e6 = QLineEdit("Hello PyQt5")
        e6.setReadOnly(True)
        flo.addRow("Read Only", e6)
        e5.editingFinished.connect(self.enterPress)
        self.setLayout(flo)
        self.setWindowTitle("QLineEdit例子")

    def textchanged(self, text):
        print("输入的内容为: "+text)

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


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = lineEditDemo()
    win.show()
    sys.exit(app.exec_())

效果:

图3 复杂的表单

6.3 QTextEdit

QTextEdit 类是一个多行文本框控件,可以显示多行文本内容,当文本内容超出控件显示范围时,可以显示水平个垂直滚动条。QTextEdit 不仅可以显示文本还可以显示HTML文档。

QTextEdit 类中的常用方法:

from PyQt5.QtWidgets import QApplication,  QWidget,  QTextEdit, QVBoxLayout
import sys


class TextEditDemo(QWidget):
    def __init__(self, parent=None):
        super(TextEditDemo, self).__init__(parent)
        self.setWindowTitle("QTextEdit 例子")
        self.resize(300, 270)
        self.textEdit = QTextEdit()
        layout = QVBoxLayout()
        layout.addWidget(self.textEdit)
        self.setLayout(layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = TextEditDemo()
    win.show()
    sys.exit(app.exec_())

效果:

图4 文本输入

你可能感兴趣的:(PyQt5 系统化学习: 文本框类控件)