一、获取、提示、设置文本
displayText 获取除NoEcho以外的文本
text 获取所有文本,包括NoEcho
setPlaceholderText 设置占位文本,无法被text或者displayText获取
setText 设置文本
# -*- coding: utf-8 -*-
from PySide6.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton
class Edit(QWidget):
def __init__(self):
super(Edit, self).__init__()
self.UI()
self.setGeometry(200, 200, 500, 300)
self.setWindowTitle("QLineEdit")
self.show()
def UI(self):
self.line = QLineEdit("请输入内容", self)
self.btn = QPushButton("获取文本", self)
self.btn.setGeometry(50, 120, 200, 30)
# ///
self.label_1 = QLineEdit(self)
self.label_1.setGeometry(50, 50, 200, 30)
self.label_1.setPlaceholderText("账号")
self.btn.clicked.connect(self.txt)
def txt(self):
print("line text", self.line.text())
print("displayText ", self.label_1.displayText())
print("label_2 text", self.label_1.text())
if __name__ == '__main__':
app = QApplication([])
edit = Edit()
app.exec()
二、显示模式
QLineEdit.EchoMode.Normal 正常显示用户输入的文本
QLineEdit.EchoMode.NoEcho 不会显示任何输入的文本
QLineEdit.EchoMode.Password 用户输入时显示一个密码字符
QLineEdit.EchoMode.PasswordEchoOnEdit 开始输入时显示密码字符
from PySide6.QtWidgets import QApplication, QWidget, QLineEdit
class Edit(QWidget):
def __init__(self):
super(Edit, self).__init__()
self.UI()
self.setGeometry(200, 200, 500, 300)
self.setWindowTitle("QLineEdit")
self.show()
def UI(self):
self.label_1 = QLineEdit(self)
self.label_2 = QLineEdit(self)
self.label_3 = QLineEdit(self)
self.label_4 = QLineEdit(self)
self.label_1.setGeometry(50, 50, 200, 30)
self.label_1.setPlaceholderText("账号")
# 按字符输入时的形式显示。默认值。
self.label_1.setEchoMode(QLineEdit.EchoMode.Normal)
self.label_2.setGeometry(50, 90, 200, 30)
self.label_2.setPlaceholderText("密码")
# 不显示任何内容。常见场景:密码的长度也需要被保护
self.label_2.setEchoMode(QLineEdit.EchoMode.NoEcho)
self.label_3.setGeometry(50, 130, 200, 30)
self.label_3.setPlaceholderText("确认密码")
# 显示时用平台决定的密码掩码字符替代真实输入的字符
self.label_3.setEchoMode(QLineEdit.EchoMode.Password)
self.label_4.setGeometry(50, 170, 200, 30)
self.label_4.setPlaceholderText("邮箱")
# 当字符正在被编辑时显示
self.label_4.setEchoMode(QLineEdit.EchoMode.PasswordEchoOnEdit)
if __name__ == '__main__':
app = QApplication([])
edit = Edit()
app.exec()
三、限制操作
setMaxLength(length: int) 设置文本最大长度,默认值为32767
setReadOnly(yes: bool) 设置是否开启只读模式,默认为False
from PySide6.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton
class Edit(QWidget):
def __init__(self):
super(Edit, self).__init__()
self.UI()
self.setGeometry(200, 200, 500, 300)
self.setWindowTitle("QLineEdit")
self.show()
def UI(self):
self.label_1 = QLineEdit(self)
self.label_1.setGeometry(50, 50, 200, 30)
self.label_1.setMaxLength(11) # 设置最大长度为11、
self.label_1.setReadOnly(True)
if __name__ == '__main__':
app = QApplication([])
edit = Edit()
app.exec()
四、文本框其他好玩的操作
.setClearButtonEnabled(enable: bool) 在编辑器内添加清空按钮
.isClearButtonEnabled() -> bool 是否启用了清空按钮
.setDragEnabled(b: bool) 设置是否允许拖拽,默认不允许
from PySide6.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton
class Edit(QWidget):
def __init__(self):
super(Edit, self).__init__()
self.UI()
self.setGeometry(200, 200, 500, 300)
self.setWindowTitle("QLineEdit")
self.show()
def UI(self):
self.label_1 = QLineEdit(self)
self.label_1.setGeometry(50, 50, 200, 30)
self.label_1.setPlaceholderText("账号") # 占位文本
self.label_1.setClearButtonEnabled(True) # 在编辑器内添加清空按钮
self.label_1.isClearButtonEnabled() # 是否启用了清空按钮
self.label_1.setDragEnabled(True) # 设置是否允许拖拽
if __name__ == '__main__':
app = QApplication([])
edit = Edit()
app.exec()
LineEdit的读取与写入(后台计算)
filename = QFileDialog.getOpenFileName(self, "选取文件", " ", "Text files (*.txt)")
print(filename[0])
# print(type(filename[0])) # str
self.report_UI.lineEdit_4.setText(filename[0]) # 将计算的结果生成在LineEdi 里
提示:注意的是必须将计算的结果转换为字符型,否则程序运行不起来