PyQt ------ QTextEditor

PyQt ------ QTextEditor

  • 引言
  • 正文
    • 示例1------进阶示例

引言

这里给大家介绍一下 PyQt6 中的 QTextEditor 组件用法。

正文

QTextEditor 可以进行多行字符串输出的组件。

想要获取 QTextEditor 组件中当前存放的字符串,需要使用:

QTextEditor.toPlainText()

想要对 QTextEditor 组件中的字符串进行更新,需要使用:

QTextEditor.setText()

示例1------进阶示例

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout, QPushButton


class TextEditorDemo(QWidget):
    def __init__(self, parent=None):
        super(TextEditorDemo, self).__init__(parent)
        self.setWindowTitle('QTextEditor Example')

        self.text_editor = QTextEdit()
        self.text_editor.setText(
            """JiJi is smart. \nShe is also beautiful.\nShe is 26 years old"""
        )
        self.text_editor.setFixedSize(400, 200)

        button = QPushButton()
        button.setText('Change QTextEdit message')
        button.clicked.connect(self.button_clicked)

        layout = QVBoxLayout(self)
        layout.addWidget(self.text_editor)
        layout.addWidget(button)

    def button_clicked(self):
        self.text_editor.setText('Do you want to be her friend?\n'
                                 "Do you want to go outside with her?")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = TextEditorDemo()
    win.show()
    sys.exit(app.exec())

运行后的界面如下:
PyQt ------ QTextEditor_第1张图片
点击后得到的界面如下:
PyQt ------ QTextEditor_第2张图片
如果大家觉得有用,就请点个赞吧~

你可能感兴趣的:(PyQt5,pyqt)