要向TextEdit输入字符串,可以使用setText()函数将字符串设置为TextEdit的文本内容。如果要在现有文本的末尾添加字符串,可以使用append()函数。

以下是一个示例代码:

from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.text_edit = QTextEdit(self)
        self.setCentralWidget(self.text_edit)

        self.setWindowTitle("Text Editor")

        # 设置初始文本
        self.text_edit.setText("Hello World")

        # 在现有文本的末尾添加字符串
        self.text_edit.append("This is a new line.")

if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()

在上面的示例中,我们创建了一个名为MainWindow的主窗口类,并在其构造函数中创建了一个QTextEdit小部件。然后,我们将QTextEdit小部件设置为主窗口的中央小部件,并设置了初始文本为"Hello World"。接下来,我们使用append()函数在现有文本的末尾添加了一行新的字符串" This is a new line."。

运行上述代码,将会打开一个带有初始文本和新添加的字符串的TextEdit窗口。

QT如何继续向TextEdit输入字符串

原文地址: https://www.cveoy.top/t/topic/iAxF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录