I have a python program that wraps another program I’m running with subprocess.run(). The program has a great console output that I’d like to show in the GUI,
I’m struggling to figure out how to capture this output and show it in the GUI. I’m using threading to run the subprocess routine so that the tkinter for the GUI remains responsive and updates as the process runs which can take hours. Print statements are being shown in the GUI, but not the output of the subprocess. I’d like a high level view of how this will work.
I thought if I redirected the stdout in the subprocess call that it should have worked. I’m not sure what I’ve got wrong.
The class that redirects the print statements to the GUI:
class RedirectOutput:
def __init__(self, text_widget):
self.text_widget = text_widget
def write(self, message):
self.text_widget.config(state='normal')
self.text_widget.insert(tk.END, message)
self.text_widget.config(state='disabled')
self.text_widget.see(tk.END)
def flush(self):
pass
# Redirect stdout to the ScrolledText widget
sys.stdout = RedirectOutput(self.output_text)
# Example of subprocess
subprocess.run([BASE_COMMAND[0], BASE_COMMAND[1], BASE_COMMAND[2],
"upload",
str(upload_file),
destination_path],
stdout=subprocess.PIPE,
creationflags=subprocess.CREATE_NO_WINDOW,
shell=True,)