python 重定向输出到控件

class RedirectText(io.TextIOBase):
    def __init__(self):
        self.browser = None

    def setTextBrowser(self, browser):
        self.browser = browser

    def write(self, text):

        scroll_bar = self.browser.verticalScrollBar()
        scroll_bar.setSliderPosition(scroll_bar.maximum())

        text_cursor = self.browser.textCursor()
        text_cursor.movePosition(QTextCursor.End)
        text_cursor.insertText(text)

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        redirect_text = RedirectText()
        # 保存原来的标准输出
        original_stdout = sys.stdout
        # 重定向标准输出到自定义的写入器
        sys.stdout = redirect_text
        redirect_text.setTextBrowser(self.ui.plainTextEdit)