Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I assume that with "output from the interpreter", you mean output written to the console or terminal window, such as output produced with <code>print()</code>.</p> <p>All console output produced by Python gets written to the program's output streams <code>sys.stdout</code> (normal output) and <code>sys.stderr</code> (error output, such as exception tracebacks). These are file-like objects.</p> <p>You can replace these streams with your own file-like object. All your custom implementation must provide is a <code>write(text)</code> function. By providing your own implementation, you can forward all output to your widget:</p> <pre><code>class MyStream(object): def write(self, text): # Add text to a QTextEdit... sys.stdout = MyStream() sys.stderr = MyStream() </code></pre> <p>If you ever need to reset these streams, they are still available as <code>sys.__stdout__</code> and <code>sys.__stderr__</code>:</p> <pre><code>sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ </code></pre> <hr> <p><strong>Update</strong></p> <p>Here is some working code for PyQt4. First define a stream that reports data written to it with a Qt signal:</p> <pre><code>from PyQt4 import QtCore class EmittingStream(QtCore.QObject): textWritten = QtCore.pyqtSignal(str) def write(self, text): self.textWritten.emit(str(text)) </code></pre> <p>Now, in your GUI, install an instance of this stream to <code>sys.stdout</code> and connect the <code>textWritten</code> signal to a slot that writes the text to a <code>QTextEdit</code>:</p> <pre><code># 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() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload