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.

share|improve this question
    
iterate over stdout after piping – Padraic Cunningham Apr 7 at 21:36
    
What do you mean by that? – user2893045 Apr 7 at 21:37
    
You need to add a runnable example, I have no idea what your code is doing as, returning 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
    
You probably want to run use a Queue with thread but it completely depends on what you are using. A runnable example as I said before would help or at least what library you are using – Padraic Cunningham Apr 7 at 21:46
    
I edited my code to include the piping argument you listed above. I now have information showing up in my GUI, but only after the script finishes running (I have a test script that just prints a few lines, but the main one is much longer and also requires inputs). It also freezes. – user2893045 Apr 7 at 21:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.