PyQt ------ QLineEditor
- 引言
- 正文
- 示例1------基础示例
- 示例2------进阶示例
引言
这里给大家介绍一下 PyQt6 中的 QLineEditor 组件用法。
正文
QLineEditor 是用于做单行字符串输出的组件,它只能够将字符串在一行进行输出,如果要进行多行输出,请参考 PyQt ------ QTextEditor。
想要获取 QLineEditor 组件中当前存放的字符串,需要使用:
QLineEditor.text()
想要对 QLineEditor 组件中的字符串进行更新,需要使用:
QLineEditor.setText()
示例1------基础示例
请看如下代码:
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLineEdit, QHBoxLayout
class LineEditDemo(QWidget):
def __init__(self, parent=None):
super(LineEditDemo, self).__init__(parent)
self.setWindowTitle('QLineEdit Example')
line_editor = QLineEdit()
line_editor.setText("JiJi is smart.")
line_editor.setFixedSize(400, 20)
layout = QHBoxLayout(self)
layout.addWidget(line_editor)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = LineEditDemo()
win.show()
sys.exit(app.exec())
需要注意我们这里使用的是最新的 PyQt6 模块,因此,最后一行使用的是 app.exec() 而不是 app.exec_()。
运行后我们可以得到如下所示的界面:

示例2------进阶示例
接下来,我们通过点击按键对 QLineEdit() 中的值进行更改。
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLineEdit, QVBoxLayout, QPushButton
class LineEditDemo(QWidget):
def __init__(self, parent=None):
super(LineEditDemo, self).__init__(parent)
self.setWindowTitle('QLineEdit Example')
self.line_editor = QLineEdit()
self.line_editor.setText("JiJi is smart.")
self.line_editor.setFixedSize(400, 20)
button = QPushButton()
button.setText('Change QLineEditor message')
button.clicked.connect(self.button_clicked)
layout = QVBoxLayout(self)
layout.addWidget(self.line_editor)
layout.addWidget(button)
def button_clicked(self):
self.line_editor.setText('She is also beautiful.')
if __name__ == '__main__':
app = QApplication(sys.argv)
win = LineEditDemo()
win.show()
sys.exit(app.exec())
在未点击之前, Gui 界面如下:

点击按键之后:

如果大家觉得有用,就请点个赞吧~







![[数据结构]顺序表](https://img-blog.csdnimg.cn/direct/9515a66a2cef428d92bf953a574d372a.png)











