1

There are similar questions to this one, but I'd like to see a clarified answer. I'm building a simple GUI with PythonCard to wrap a command line process. Specifically, it's a wrapper for a series of ANT Tasks and other custom operations so non-devs can use it.

I'd like to redirect the output of the subprocess to a TextArea in the window. It looks like the way to do this is to use subprocess.Popen(command, stdout=subprocess.PIPE) and load the output to a variable.

The question is how do I live update the window with the output of the subprocess? Any hints would be welcome

Thanks

2 Answers 2

1

Just about every subprocess you can wrap will buffer its output unless you manage to fool it into believing it's actually connected to a terminal -- and subprocess can't do that. Rather, look into pexpect (runs well on every platform that lets you have a pseudoterminal, i.e., every platform except Microsoft Windows; on Windows you might try wexpect but I have no experience with the latter).

These modules give you the subprocess's output as soon as it's produced, and strive to fool the module into producing that output ASAP and without buffering, so they should make it easy for you to receive that output in real time and append it to the text field you want to keep updating.

Sign up to request clarification or add additional context in comments.

Comments

1

I was searching for a solution for this too. It turns out the solution is remarkably simple:

proc = subprocess.Popen("whatever program", cwd="startup dir", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
while True:
txt = proc.stdout.readline()
if not txt: break
txt=txt.replace("\r\n","\n").replace("\r\n","\n").replace("\\","\")
self.components.taStdout.appendText(txt)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.