pyqt5-单行文本框QLineEdit

简介

QLineEdit是一个单行文本框。

方法

方法说明
setText()设置文本框内容
text()获取文本框内容
setPlaceholderText()设置文本框浮显文字
setAlignment()设置文本对齐方式
setReadOnly()设置文本框只读
setFocus()使文本框得到焦点
setEchoMode()设置文本框的显示格式 QLineEdit.Normal QLineEdit.Password QLineEdit.NoEcho QLineEdit.PasswordEchoOnEdit
setValidator()

设置文本框验证器,有以下三种模式

QIntValidator: 限制输入整数

QDoubleValidator: 限制输入小数

QRegExpValidator: 检查输入是否符合设置的正则表达式

setInputMask()设置掩码
clear()清除文本框内容

信号

1.selectionChanged        选择改变

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

2.textChanged  文本改变

当修改文本内容的时候,信号会被发射

3.editingFinished        编辑结束

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

程序和结果截图

import sys

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


class LineDemo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        # 定义文本行
        lineEdit_normal = QLineEdit()

        lineEdit_password = QLineEdit()
        lineEdit_noEcho = QLineEdit()
        lineEdit_passwordEchoOnEdit = QLineEdit()

        # 设置文本行的显示模式
        lineEdit_normal.setEchoMode(QLineEdit.Normal)
        lineEdit_password.setEchoMode(QLineEdit.Password)
        lineEdit_noEcho.setEchoMode(QLineEdit.NoEcho)
        lineEdit_passwordEchoOnEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)

        # 设置文本行的浮显文字
        lineEdit_normal.setPlaceholderText("Normal")
        lineEdit_password.setPlaceholderText("Password")
        lineEdit_noEcho.setPlaceholderText("NoEcho")
        lineEdit_passwordEchoOnEdit.setPlaceholderText("PasswordEchoOnEdit")

        # 定义布局,并向布局中添加部件
        formLayout = QFormLayout()
        formLayout.addRow("Normal", lineEdit_normal)
        formLayout.addRow("Password", lineEdit_password)
        formLayout.addRow("NoEcho", lineEdit_noEcho)
        formLayout.addRow("PasswordEchoOnEdit", lineEdit_passwordEchoOnEdit)

        # 设置布局
        self.setLayout(formLayout)


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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值