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?
I have a program in Python with PyQt, designed to run on Windows. This
Share
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) andsys.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:If you ever need to reset these streams, they are still available as
sys.__stdout__andsys.__stderr__:Update
Here is some working code for PyQt4. First define a stream that reports data written to it with a Qt signal:
Now, in your GUI, install an instance of this stream to
sys.stdoutand connect thetextWrittensignal to a slot that writes the text to aQTextEdit: