QTextEdit控件的作用
QTextEdit是一个多行文本框控件,可以显示多行文本内容,当文本内容超出控件显示范围时,可以显示水平个垂直滚动条。
QTextEdit不仅可以显示文本还可以显示HTML文档
代码示例
# -*- coding: utf-8 -*- from PyQt5.QtWidgets import QApplication, QWidget , QTextEdit, QVBoxLayout , QPushButton import sys class TextEditDemo(QWidget): def __init__(self, parent=None): super(TextEditDemo, self).__init__(parent) self.setWindowTitle("QTextEdit 例子") self.resize(250, 250) self.textEdit = QTextEdit( ) self.btnPress1 = QPushButton("显示文本") self.btnPress2 = QPushButton("显示HTML") layout = QVBoxLayout() layout.addWidget(self.textEdit) layout.addWidget(self.btnPress1) layout.addWidget(self.btnPress2) self.setLayout(layout) self.btnPress1.clicked.connect(self.btnPress1_Clicked) self.btnPress2.clicked.connect(self.btnPress2_Clicked) def btnPress1_Clicked(self): self.textEdit.setPlainText("Hello PyQt5!\n点击按钮") def btnPress2_Clicked(self): self.textEdit.setHtml("<font color='red' size='6'><red>Hello PyQt5!\n点击按钮。</font>") if __name__ == "__main__": app = QApplication(sys.argv) win = TextEditDemo() win.show() sys.exit(app.exec_())
运行结果:
点击显示文本按钮,运行结果:
点击显示HTML按钮,运行结果: