Question

I'm writing a python application that runs several subprocesses using subprocess.Popen objects. I have a glade GUI and want to display the output of these commands (running in subprocess.Popen) in the gui in real time.

Can anyone suggest a way to do this? What glade object do I need to use and how to redirect the output?

Was it helpful?

Solution 3

After lots of reading and not getting the results I wanted, I found another method that works.

It goes like this

#!/usr/bine/env python
import subprocess
import gtk

### Of course, you should have the gui built and know which widgets to use for this.
viewer = self.builder.get_widget('txtview')
proc = subprocess.Popen('ls -al /home'.split(), stdout=subprocess.PIPE, stderr = subprocess.STDOUT)
while True:
    line = proc.stdout.readline()
    viewer.get_buffer().instert_at_cursor(line)
    if not line:
       break

OTHER TIPS

Here is a link that also displays another way of doing this.

I found this to be very insightful, maybe someone else can use these tips.

http://pygabriel.wordpress.com/2009/07/27/redirecting-the-stdout-on-a-gtk-textview/

glade is only a program to build gui with gtk so when you ask for a glade object maybe you should ask for gtk widget and in this case textbuffer and textview chould be a solution or maybe treeview and liststore. subprocess.Popen has stdout and stderr arguments that can accept a file-like object. you can create an adapter that writes to the textbuffer or add items in the liststore

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top