一、setReadOnly()方法
setReadOnly()方法用于设置QLineEdit为只读模式,禁止用户编辑文本内容,语法为:
lineEdit.setReadOnly(True)
其中,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将无法编辑。
二、setPlaceholderText()方法
setPlaceholderText()方法用于设置QLineEdit中的占位符文本,当QLineEdit为空时,会显示占位符文本,语法为:
lineEdit.setPlaceholderText(text)
其中,text为需要设置的占位符文本。
演示:
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QVBoxLayout
app = QApplication([])
window = QWidget()
lineEdit = QLineEdit()
lineEdit.setPlaceholderText("Please input something...")
layout = QVBoxLayout()
layout.addWidget(lineEdit)
window.setLayout(layout)
window.show()
app.exec_()
运行后,QLineEdit中没有文本时,会显示占位符文本“Please input something...”。