Jun-06-2020, 11:05 AM
Since you are already writing to a file, it is easy to open that file and write the contents to your textbox. As Gribouillis mentions, use after() to periodically call a function that reads from the file and writes to the display.
If the file is a stopgap measure and you would rather redirect stdout, use redirect_stdout from the contextlib module.
If the file is a stopgap measure and you would rather redirect stdout, use redirect_stdout from the contextlib module.
from contextlib import redirect_stdout, redirect_stderr … class Console(QtWidgets.QWidget): """A GUI version of code.InteractiveConsole.""" ... def errorwrite(self, line): """Buffer stdout until you get a newline""" self.stdoutbuffer += line if self.stdoutbuffer [-1] == '\n': # Add line to display self.writeoutput(self.stdoutbuffer[:-1]) self.stdoutbuffer = '' def writeoutput(self, line): """Display line in outdisplay""" self.outdisplay.moveCursor(QtGui.QTextCursor.End) self.outdisplay.appendPlainText(line) self.outdisplay.moveCursor(QtGui.QTextCursor.End) ... # Redirect stdout to console.write with redirect_stdout(console): console.show() # draws the window sys.exit(app.exec_()) # Like mainloop in tkThis is from a embeddable console I use in Qt applications. Most of it should be applicable to Tk other than the mechanics of how the text is appended to the text widget (I use QPlainTextEdit widget to display stdout).