首页 PyQt5教程 QLineEdit文本编辑控件setPlaceholderText()和setReadOnly()方法教程
pay pay
教程目录

QLineEdit文本编辑控件setPlaceholderText()和setReadOnly()方法教程

日期: 四月 26, 2023, 12:26 p.m.
栏目: PyQt5教程
阅读: 174
作者: Python自学网-村长

摘要: QLineEdit文本编辑控件setPlaceholderText()和setReadOnly()方法教程

一、setPlaceholderText()方法

setPlaceholderText()方法用于在QLineEdit中设置提示文本,语法为:

lineEdit.setPlaceholderText(text)

其中,text为提示文本。

演示:

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

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

lineEdit = QLineEdit()
lineEdit.setPlaceholderText("请输入文本")

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

window.show()
app.exec_()

运行后,在QLineEdit中还未输入文本时,会显示提示文本“请输入文本”。

二、setReadOnly()方法

setReadOnly()方法用于设置QLineEdit中是否只读,语法为:

lineEdit.setReadOnly(enabled)

其中,enabled为True表示只读,False表示可编辑。

演示:

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

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

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

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

window.show()
app.exec_()

运行后,QLineEdit中的文本不能编辑。

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