PyQt5最全12 使用QTextEdit控件输入多行文本

PyQt5最全12 使用QTextEdit控件输入多行文本

from PyQt5.QtWidgets import *
import sys


class QTextEditDemo(QWidget):
    """
    QTextEditDemo控件
    """
    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.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)

        self.setLayout(layout)

    def onClick_ButtonText(self):
        self.textEdit.setPlainText('Hello World!')

    def onClick_ButtonToText(self):
        print(self.textEdit.toPlainText())

    def onClick_ButtonHTML(self):
        self.textEdit.setHtml("Hello World")

    def onClick_ButtonToHTML(self):
        print(self.textEdit.toHtml())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QTextEditDemo()
    print(main.__doc__)
    main.show()
    sys.exit(app.exec_())

结果

PyQt5最全12 使用QTextEdit控件输入多行文本_第1张图片 PyQt5最全12 使用QTextEdit控件输入多行文本_第2张图片 PyQt5最全12 使用QTextEdit控件输入多行文本_第3张图片

打印结果

Hello World
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;">
<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:x-large; color:#000000;">Hello World</span></p></body></html>

你可能感兴趣的:(GUI-PyQt5,gui,pyqt5,python)