PyQt5基本窗口控件-----QTextEdit
程序员文章站
2022-03-10 21:43:02
...
QtextEdit类可以输入多行字符串,当超过显示范围后,会显示滚动条,还可以显示HTML文档
import sys
from PyQt5.QtWidgets import *
class QTextEditDemo(QWidget):
def __init__(self):
super(QTextEditDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QTextEdit控件')
self.resize(300, 280)
self.textEdit = QTextEdit()
self.buttonText = QPushButton('显示文本')
self.buttonHTML = QPushButton('显示HTML')
self.buttonToText = QPushButton('获取文本')
self.buttonToHTML = QPushButton('获取HTML')
layout = QVBoxLayout()
layout.addWidget(self.textEdit)
layout.addWidget(self.buttonText)
layout.addWidget(self.buttonHTML)
layout.addWidget(self.buttonToText)
layout.addWidget(self.buttonToHTML)
self.setLayout(layout)
self.buttonText.clicked.connect(self.onClick_ButtonText)
self.buttonHTML.clicked.connect(self.onClick_ButtonHTML)
self.buttonToText.clicked.connect(self.onClick_ButtonToText)
self.buttonToHTML.clicked.connect(self.onClick_ButtonToHTML)
def onClick_ButtonText(self):
self.textEdit.setPlainText('hello world')
def onClick_ButtonHTML(self):
self.textEdit.setHtml('<font color="blue" size=6>hello pyqt5</font>')
def onClick_ButtonToText(self):
print(self.textEdit.toPlainText())
def onClick_ButtonToHTML(self):
print(self.textEdit.toHtml())
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QTextEditDemo()
main.show()
sys.exit(app.exec_())
下一篇: PyQt5—基本窗口控件(二)