import subprocess as sub
class GUI:
def __init__(self):
# Redirect the stdout to self.textStream
self.textStream = StringIO()
sys.stdout = self.textStream
def run(self,*args):
sub.Popen(["python",self.filename])
def doLogic(self):
self.TextBox['state'] = 'normal'
self.TextBox.insert('end',self.textStream.getvalue())
self.TextBox['state'] = 'disabled'
G = GUI()
while True:
G.root.update()
G.doLogic()
My dilemma is that I want to be able to use the 'run' function to run another python script, and to be able to capture stdout from that script in real time and display it in my GUI. The doLogic() command is there so that I can update the GUI in real time with what's happening on the console from the other script.
What happens with this code is that the text still appears in the console window, but if I do any print() commands (In the MAIN script), they show up in my GUI as you'd expect since I redirected sys.stdout.
Currently they just spam my text box since I'm just setting it over and over again to whatevers been printed, but my main concern right now is getting the text from the stdout happening in my "sub" script.
Popen(["python",self.filename],stdout=PIPE)
where PIPE is subprocess.PIPE will allow you to iterate over stdout but you will need to be careful or your gui will freeze – Padraic Cunningham Apr 7 at 21:39