如何使用 Python 将控制台输出定向到 pyqt5 qplainTextEdit 小部件?

How do I direct console output to a pyqt5 plainTextEdit widget with Python?

我正在尝试在 PyQt5 的 QplainTextEdit 小部件中显示 python 脚本的控制台输出。

我收到此错误:

TypeError: Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

我已经在 pyqt GUI 文件中定义了我的对象,我相信我有所有的导入。

更新

我修改了这个问题的代码:

from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtWidgets import QFileDialog, QPlainTextEdit
from PyQt5 import QtCore, QtGui, QtWidgets
from PIL import Image, ImageQt, ImageEnhance
# from PyQt5.QtGui import Qt
from pyqtgraph.examples.text import text

from covid19gui_V3 import Ui_MainWindow
import os
import sys

input_img = Image.open("/home/ironmantis7x/Documents/Maverick_AI/Python/keras-covid-19/maverickAI30k.png")
text_edit = QPlainTextEdit()

class EmittingStream(QtCore.QObject):

    textWritten = QtCore.pyqtSignal(str)
    def write(self, text):
        self.textWritten.emit(str(text))

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    textWritten = QtCore.pyqtSignal(str)
    def __init__(self, parent=None, **kwargs):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.ShowIButton.clicked.connect(self.do_test)
        self.chooseStudy.clicked.connect(self.do_choosestudy)
        self.RunButton_3.clicked.connect(self.do_runstudy)
        self.scene = QtWidgets.QGraphicsScene(self)
        self.graphicsView.setScene(self.scene)
        w, h = input_img.size
        self.pixmap_item = self.scene.addPixmap(QtGui.QPixmap())
        # self.graphicsView.fitInView(QRectF(0, 0, w, h), Qt.KeepAspectRatio)
        self.graphicsView.update()
        self.plainTextEdit.update()
        self.level = 1
        self.enhancer = None
        self.timer = QtCore.QTimer(interval=500, timeout=self.on_timeout)
        sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)

    def write(self, text):
        self.textWritten.emit(str(text))

    @QtCore.pyqtSlot()
    def do_test(self):
        # input_img = Image.open("/home/ironmantis7x/Documents/Maverick_AI/Python/keras-covid-19/maverickAI30k.png")
        self.enhancer = ImageEnhance.Brightness(input_img)
        self.timer.start()
        self.ShowIButton.setDisabled(True)

    @QtCore.pyqtSlot()
    def on_timeout(self):
        if self.enhancer is not None:
            result_img = self.enhancer.enhance(self.level)
            qimage = ImageQt.ImageQt(result_img)
            self.pixmap_item.setPixmap(QtGui.QPixmap.fromImage(qimage))
        if self.level > 7:
            self.timer.stop()
            self.enhancer = None
            self.level = 0
            self.ShowIButton.setDisabled(False)
        self.level = 1
        self.ShowIButton.setDisabled(False)

    @QtCore.pyqtSlot()
    def do_choosestudy(self):
        dlg = QFileDialog()
        dlg.setFileMode(QFileDialog.AnyFile)
        if dlg.exec_():
            filenames = dlg.selectedFiles()
            f = open(filenames[0], 'r')

    @QtCore.pyqtSlot()
    def do_runstudy(self):
        os.system("df -h")
        # filetext = open('screenout.txt').read()
        # filetext.close()
        # textViewValue = self.plainTextEdit.toPlainText()
        # QPlainTextEdit.appendPlainText(self, str(textViewValue))
        # sys.stdout = self(textWritten=self.textWritten)
        self.normalOutputWritten(text_edit)

    def __del__(self):
        # Restore sys.stdout
        sys.stdout = sys.__stdout__

    def normalOutputWritten(self, text_edit):
        #cursor = self.plainTextEdit.textCursor()
        #cursor.movePosition(QtGui.QTextCursor.End)
        #cursor.insertText(text_edit)
        self.plainTextEdit.appendPlainText(text_edit)
        #self.plainTextEdit.ensureCursorVisible()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

我怎样才能使它正常工作?

更新 2

我确实确实研究过这个主题,这是我在 post 提出问题之前用来尝试解决问题的主要资源之一:How to capture output of Python's interpreter and show in a Text widget?

更新 3

我修改了 post 中的代码以反映 link 中的代码建议,我曾经帮助我解决问题。

我仍然无法正确地运行。我现在收到此错误:

self.plainTextEdit.appendPlainText(text_edit) TypeError: appendPlainText(self, str): argument 1 has unexpected type 'QPlainTextEdit'

我有一个用户界面 TableManagerWindow,我一直在 Qt 设计器中维护和开发它。通过 pyuic 转换为 *.py 文件后,我能够实现 Ferdinand Beyer 在您上面提供的 link 中建议的内容。将文本打印到终端的简单按钮,它确实通过 append() 附加到 QTextEdit 小部件。出于某种原因不确定这是否适合您,但我可以保证它也适用于我。我不够聪明,无法理解导致您出现问题的细微差别,但我想我会把它放在这里以防万一。如果它无关紧要,管理员可以随意删除它,但它有效。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

# Define a stream, custom class, that reports data written to it, with a Qt signal
class EmittingStream(QtCore.QObject):

    textWritten = QtCore.pyqtSignal(str)

    def write(self, text):
        self.textWritten.emit(str(text))

class Ui_TableManagerWindow(object):
    def setupUi(self, TableManagerWindow):
        #define all of my widgets, layout, etc here
        .
        .
        .
        # Install a custom output stream by connecting sys.stdout to instance of EmmittingStream.
        sys.stdout = EmittingStream(textWritten=self.output_terminal_written)

        # Create my signal/connections for custom method
        self.source_dir_button.clicked.connect(self.sourceDirButtonClicked)

        self.retranslateUi(TableManagerWindow)
        QtCore.QMetaObject.connectSlotsByName(TableManagerWindow)


    def retranslateUi(self, TableManagerWindow):
        .
        .
        .

    #custom method that prints to output terminal.  The point is to have this emmitted out to my QTextEdit widget.
    def sourceDirButtonClicked(self):
        for i in range(10):
            print("The Source DIR button has been clicked " + str(i) + " times")

    #custom method to write anything printed out to console/terminal to my QTextEdit widget via append function.
    def output_terminal_written(self, text):
        self.output_terminal_textEdit.append(text)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    TableManagerWindow = QtWidgets.QMainWindow()
    ui = Ui_TableManagerWindow()
    ui.setupUi(TableManagerWindow)
    TableManagerWindow.show()
    sys.exit(app.exec_())