tags:

views:

218

answers:

2

I'm starting to work with Ubuntu's "quickly" framework, which is python/gtk based. I want to write a gui wrapper for a textmode C state-machine that uses stdin/stdout.

I'm new to gtk. I can see that the python print command will write to the terminal window, so I assume I could redirect that to my C program's stdin. But how can I get my quickly program to monitor stdin (i.e. watch for the C program's stdout responses)? I suppose I need some sort of polling loop, but I don't know if/where that is supported within the "quickly" framework.

Or is redirection not the way to go - should I be looking at something like gobject.spawn_async?

+1  A: 

I'm not sure about the quickly framework, but in Python you can use the subprocess module which spawns a new child process but allows communication via stdin/stdout.

http://docs.python.org/library/subprocess.html

Take a look at the documentation, but that's pretty useful.

If you want to do polling you can use a gobject.timeout_add.

You'd create a function something like this:

def mypoller(self):
    data = myproc.communicate()
    if data[0]: #There's data to read
       # do something with data
    else:
       # Do something else - delete data, return False
       # to end calls to this function

and that would let you read data from your process.

Wayne Werner
Wow... thanks for fast response. I'll read up on it.
neil
+2  A: 

The gtk version of select, is glib.io_add_watch, you may want to redirect the stdin/stdout of the process to/from the GUI, you can check an article I've written time ago:

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

pygabriel
Wow... thanks for fast response. I'll read your article.
neil