PyQt5——文本框类控件

文本框类控件


QLineEdit

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

常用的方法

  • setAlignment() 按照固定值对齐文本

    • Qt.AlignLeft 水平方向靠左对齐
    • Qt.AlignRight 水平方向靠右对齐
    • Qt.AlignCenter 水平方向居中对齐
    • Qt.AlignJustify 水平方向调整间距两端对齐
    • Qt.AlignTop 垂直方向靠上对齐
    • Qt.AlignBottom 垂直方向靠下对齐
    • Qt.AlignVCenter 垂直方向居中对齐
  • clear() 清除文本框内容

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

    • QLineEdit.Normal 正常显示所输入的字符,默认选项
    • QLineEit.NoEcho 不显示任何输入的字符,常用于密码类型的输入,且其密码长度需要保密时
    • QLineEdit.Password 显示与平台相关的密码掩码字符,而不是实际输入的字符
    • QLineEdit.PassswordEchoOnEdit 在编辑时显示字符,负责显示密码类型的输入。
  • setPlacholderText() 设置文本框浮显文字

  • setMaxLength() 设置文本框所允许输入的最大字符数

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

  • setText() 设置文本框内容

  • Text() 返回文本框内容

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

  • selectAll() 全选

  • setFocus() 得到焦点

  • setInputMask() 设置掩码

  • setValidator 设置文本框的验证器(验证规则)将限制任意可能输入的文字

    • QIntValidator 限制输入整数
    • QDoubleValidator 限制输入浮点数
    • QRegexpValidator 检查输入是否符合正则表达式

定义输入掩码的字符
在这里插入图片描述
在这里插入图片描述

QLineEdit常用的信号

  • selectionChanged 只要选择改变了,这个信号就会被发射
  • textChanged 当修改文本内容时,这个信号就会被发射
  • editingFinished 当编辑文本结束时,这个信号就会被发射

EchoMode 的显示效果

# -*- coding: utf-8 -*-

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("PasswordEchoEdit")

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

在这里插入图片描述

验证器

# -*- coding: utf-8 -*-

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_())

在这里插入图片描述

输入掩码

# -*- coding: utf-8 -*-

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)

        self.setLayout(flo)

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

在这里插入图片描述

综合示例

# -*- coding: utf-8 -*-

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)

        el = QLineEdit()
        el.setValidator(QIntValidator())
        el.setMaxLength(4)
        el.setAlignment(Qt.AlignRight)
        el.setFont(QFont("Arial", 20))

        e2 =  QLineEdit()
        e2.setValidator(QDoubleValidator(0.99, 99.99, 2))

        flo = QFormLayout()
        flo.addRow("integer validator", el)
        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)
        e5.editingFinished.connect(self.enterPress)
        flo.addRow("Password", e5)

        e6 = QLineEdit("Hello PyQt5")
        e6.setReadOnly(True)
        flo.addRow("Read Only", e6)

        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_())

在这里插入图片描述

输入的内容为:2
输入的内容为:23
输入的内容为:232
输入的内容为:2323
输入的内容为:23233
输入的内容为:232335
输入的内容为:2323356
输入的内容为:23233568
输入的内容为:232335689
已输入值

QTextEdit

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

常用的方法

  • setPlainText() 设置多行文本框的文本内容
  • toPlainText() 返回多行文本框的文本内容
  • setHtml 设置多行文本内容为HTML文档,
  • totHtml 返回多行文本框的HTML文档内容
  • clear() 清楚多行文本框的内容

QTextEdit的使用

# -*- coding: utf-8 -*-

from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout, QPushButton
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()
        self.btnPress1 = QPushButton("显示文本")
        self.btnPress2 = QPushButton("显示HTML")
        layout = QVBoxLayout()
        layout.addWidget(self.textEdit)
        layout.addWidget(self.btnPress1)
        layout.addWidget(self.btnPress2)
        self.setLayout(layout)
        self.btnPress1.clicked.connect(self.btnPress1_Clicked)
        self.btnPress2.clicked.connect(self.btnPress2_Clicked)

    def btnPress1_Clicked(self):
        self.textEdit.setPlainText("Hello PyQt5!\n单击按钮")

    def btnPress2_Clicked(self):
        self.textEdit.setHtml("<font color='red' size = '6'> <red> Hello PyQt5!\n单击按钮。</font>")

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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值