文本框类控件

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

QLineEdit类中的常用方法:

  setAlignment()       按固定值方式对齐文本:

                  Qt.AlignLeft, 水平方向靠左对齐

                  Qt.AlignRight,水平方向靠右对齐

                  Qt.AlignCenter,水平方向居中对齐

                                             Qt.AlignJustify,水平方向调整间距两端对齐

                                             Qt.AlignTop,垂直方向靠上对齐

                                             Qt.AlignBottom,垂直方向靠下对齐

                                             Qt.AlignVCenter,垂直方向居中对齐

  clear()                    清除文本框内容

  setEchoMode()      设置文本框显示格式。允许输入的文本显示格式的值也可以是:

                 QLineEdit.Normal,正常显示所输入的字符,此为默认选项

              QLineEdit.NoEcho,不显示任何输入的字符,常用于密码类型的输入,且其密码长度需要保密时

              QLineEdit.Password,显示与平台相关的密码掩码字符,而不是实际输入的字符

              QLineEdit.PasswordEchoEdit,在编辑时显示字符,负责显示密码类型的输入

  setPlaceholderText()  设置文本框显文字

  setMaxLength()      设置文本框所运行输入的最大字符数

  setReadOnly()       设置文本框为只读的

  setText()            设置文本框内容

  Text()                      返回文本框内容

  setDragEnabled()   设置文本框是否接受拖动

  setAll()                    全选

  setFocus()             得到焦点

  setInputMask()       设置掩码

  setValidator()         设置文本框的验证器(验证规则),将限制任意可能输入的文本。可用的校验器为:

              QIntValidator,限制输入整数

            QDoubleValidator,限制输入浮点数

            QRegexpValidator,检查输入是否符合正则表达式

QLineEdit类中的常用信号

selectionChanged            只要选择改变了,这个信号就会被发射

textChanged                    当修改文本内容时,这个信号会被发射

editingFinished                当编辑文本结束时,这个信号会被发射

案例9  EchoMode的显示效果

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


class LineEditDemo(QWidget):
    def __init__(self, parent=None):
        super().__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("PasswordEchoNoEdit")

        # 设置显示效果
        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_())

案例10  验证器

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


class LineEditDemo(QWidget):
    def __init__(self, parent=None):
        super().__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()
        pIntValidator.setRange(1, 99)

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

        # 字母和数字
        reg = QRegExp("[a-zA-Z0-9]+$")
        pValidator = QRegExpValidator()
        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_())

案例11  输入掩码

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

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


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

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

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

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

        self.setLayout(flo)


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

案例12  综合示例

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


class LineEditDemo(QWidget):
    def __init__(self, parent=None):
        super().__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_())

               

猜你喜欢

转载自www.cnblogs.com/lynsha/p/13403162.html