Skip to content Skip to sidebar Skip to footer

Python, Quickly And Glade, Showing Stdout In A Textview

I've spent ages looking for a way to do this, and I've so far come up with nothing. :( I'm trying to make a GUI for a little CLI program that I've made - so I thought using Ubuntu

Solution 1:

But I just need to know how to have this be called each time there is a new line of stdout.

You could use GObject.io_add_watch to monitor the subprocess output or create a separate thread to read from the subprocess.

# read from subprocessdefread_data(source, condition):
    line = source.readline() # might blockifnot line:
        source.close()
        returnFalse# stop reading# update text
    label.set_text('Subprocess output: %r' % (line.strip(),))
    returnTrue# continue reading
io_id = GObject.io_add_watch(proc.stdout, GObject.IO_IN, read_data)

Or using a thread:

# read from subprocess in a separate threaddefreader_thread(proc, update_text):
    with closing(proc.stdout) as file:
        for line initer(file.readline, b''):
            # execute update_text() in GUI thread
            GObject.idle_add(update_text, 'Subprocess output: %r' % (
                    line.strip(),))

t = Thread(target=reader_thread, args=[proc, label.set_text])
t.daemon = True# exit with the program
t.start()

Complete code examples.

Post a Comment for "Python, Quickly And Glade, Showing Stdout In A Textview"