首页 PyQt5教程 QLineEdit控件clear()和setMaxLength()方法
pay pay
教程目录

QLineEdit控件clear()和setMaxLength()方法

日期: 四月 25, 2023, 9:50 a.m.
栏目: PyQt5教程
阅读: 162
作者: Python自学网-村长

摘要: clear()方法用于清除QLineEdit中的文本内容,setMaxLength()方法用于设置QLineEdit中可输入的最大字符数

一、clear()方法

clear()方法用于清除QLineEdit中的文本内容,语法为:

lineEdit.clear()

演示:

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

app = QApplication([])
window = QWidget()

lineEdit = QLineEdit()
lineEdit.setText("Hello World!")

layout = QVBoxLayout()
layout.addWidget(lineEdit)
window.setLayout(layout)

window.show()

# 清除QLineEdit中的文本内容
lineEdit.clear()

app.exec_()

运行后,QLineEdit中的文本将被清除。

二、setMaxLength()方法

setMaxLength()方法用于设置QLineEdit中可输入的最大字符数,语法为:

lineEdit.setMaxLength(length)

其中,length为可输入的最大字符数。

演示:

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

app = QApplication([])
window = QWidget()

lineEdit = QLineEdit()
lineEdit.setMaxLength(10)

layout = QVBoxLayout()
layout.addWidget(lineEdit)
window.setLayout(layout)

window.show()
app.exec_()

运行后,QLineEdit中最多可输入10个字符。

部分文字内容为【Python自学网】原创作品,转载请注明出处!视频内容已申请版权,切勿转载!
回顶部