tags:

views:

433

answers:

3

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?

+1  A: 

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

mg
I stand corrected... I did mean gtk widget.Now, about this adapter, can you provide more details? Do you mean like a timer object that constantly reads the stdout and stderr and writes it to the gtk widget? or do you have something different in mind?
M0E-lnx
i was thinking that Popen writes output to the file-object instead it uses .fileno() method and os.fdopen() to that file descriptor (and i don't know why). so you can use .communicate() that reads all the output in memory until the process exits.If this is not an option for you, you shuold use PIPE and constantly reading from it (maybe from a thread). hope this helped more
mg
A: 

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
M0E-lnx
+2  A: 

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/

M0E-lnx
+1: for `gobject.io_add_watch`-based solution.
J.F. Sebastian