Search code examples
pythonpyqtwxwidgets

Wxwidgets and Pyqt


Is there a similar function PyOnDemandOutputWindow in Pyqt? This function redirect the console output to a separate window.


Solution

  • It is possible to replace sys.std[out|err] with a wrapper that writes all output to e.g. a QPlainTextEdit. A very basic example:

    class StdoutWrapper(object):
        def __init__(self, outwidget):
            self.widget = outwidget
            self.widget.setReadOnly(True) # assuming QPlainTextEdit
            self.widget.hide()
    
        def write(self, s):
            self.widget.show()
            self.widget.appendPlainText(s) # again assuming QPlainTextEdit
    

    And somewhere else:

    import sys
    
    sys.stdout = StdoutWrapper(yourwidget)
    # similar for stderr, but you might want an error dialog or make 
    # the text stand out using appendHtml