How to capture output of Python's interpreter and show in a Text widget?

Question:

I have a program in Python with PyQt, designed to run on Windows.
This program makes a lot of operations and prints a lot of info.
But as I want to freeze it and don’t want the prompt screen to appear, I want that all that info appears in the main application, in a QTextEdit or so.
How can i make the program work so it gets the output from the interpreter and shows it on the textEdit at the same time, just like it does on the real interpreter?

Asked By: jonathan.hepp

||

Answers:

I suggest you use the logging library. http://docs.python.org/library/logging.html
You could write your own log handler for communicating with the QTextEdit. Here’s a nice tutorial which will get you started: http://pantburk.info/?blog=77

Answered By: Alex Plugaru

I assume that with “output from the interpreter”, you mean output written to the console or terminal window, such as output produced with print().

All console output produced by Python gets written to the program’s output streams sys.stdout (normal output) and sys.stderr (error output, such as exception tracebacks). These are file-like objects.

You can replace these streams with your own file-like object. All your custom implementation must provide is a write(text) function. By providing your own implementation, you can forward all output to your widget:

class MyStream(object):
    def write(self, text):
        # Add text to a QTextEdit...

sys.stdout = MyStream()
sys.stderr = MyStream()

If you ever need to reset these streams, they are still available as sys.__stdout__ and sys.__stderr__:

sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__

Update

Here is some working code for PyQt4. First define a stream that reports data written to it with a Qt signal:

from PyQt4 import QtCore

class EmittingStream(QtCore.QObject):

    textWritten = QtCore.pyqtSignal(str)

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

Now, in your GUI, install an instance of this stream to sys.stdout and connect the textWritten signal to a slot that writes the text to a QTextEdit:

# Within your main window class...

def __init__(self, parent=None, **kwargs):
    # ...

    # Install the custom output stream
    sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)

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

def normalOutputWritten(self, text):
    """Append text to the QTextEdit."""
    # Maybe QTextEdit.append() works as well, but this is how I do it:
    cursor = self.textEdit.textCursor()
    cursor.movePosition(QtGui.QTextCursor.End)
    cursor.insertText(text)
    self.textEdit.setTextCursor(cursor)
    self.textEdit.ensureCursorVisible()
Answered By: Ferdinand Beyer

Unfortunatelly the example doesn’t work with PySide. It gives the following error:

sys.stdout = EmittingStream(textWritten=self.write2Console)
AttributeError: 'textWritten()' is not a Qt property or a signal

We need to make the following changes for it to work with PySide:

sys.stdout = EmittingStream()
self.connect(sys.stdout,QtCore.SIGNAL('textWritten(QString)'),self.write2Console)
Answered By: TommyX

I posted some stuff for a terminal application for PySide on this a while back Terminal like app in PySide. If You are looking at PyQt then check for PySide as well. They are basically the same thing except for licensing and a few differences in syntax.

Answered By: justengel

Thank you Ferdinand and Tommy. I have posted my new window solution for PySide6, since this thread comes up often in searches. I wanted to create a "loading" window when I have a QThread doing a bunch of stuff in the background. I create this window when I start the thread and then connect .close() from the completion of the thread. I got the closeEvent idea from a different SO Q&A. I can run my GUI from a Windows terminal, see the usual stdout in the terminal, call my loading thread function, the window pops up, the stdout goes to the window, then when it is done the window closes and the stdout returns to the terminal.

class StdOutWindow(QtWidgets.QWidget):
    def __init__(self):
        super(StdOutWindow, self).__init__()
        layout = QtWidgets.QVBoxLayout()
        self.setWindowTitle('Loading Data')
        self.txt_edit = QtWidgets.QTextEdit()
        layout.addWidget(self.txt_edit)
        self.setLayout(layout)
        self.setMinimumSize(600, 300)

        sys.stdout = EmittingStream()
        sys.stdout.text_written.connect(self.normal_output_written)

    def closeEvent(self, event: QtGui.QCloseEvent) -> None:
        sys.stdout = sys.__stdout__
        event.accept()

    def normal_output_written(self, text):
        cursor = self.txt_edit.textCursor()
        cursor.movePosition(QtGui.QTextCursor.End)
        cursor.insertText(text)
        self.txt_edit.setTextCursor(cursor)
        self.txt_edit.ensureCursorVisible()

class EmittingStream(QtCore.QObject):
    text_written = QtCore.Signal(str)

    def write(self, text):
        self.text_written.emit(str(text))
Answered By: Eric M
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.