I tried to do this and really struggled. I could not get the first example to work and quickly switched to the second.
Using the 2nd example above (using a label to display the text) and found that this works well with Python 2.7, but I'm trying to use Python3, and some things just don't work.
I struggled for ages trying to upgrade to Python3, and I had to change some import operations and change gtk to Gtk, which made it work mostly, but I was really puzzled that Python3 used utf-8 codes. I finally started working by changing the "non_block_read" function, changing the returned text from utf-8 to a string, and dealing with the returned None register.
Hope this helps.
For completeness, I enclose my working code: -
#!/usr/bin/env python3 import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk from gi.repository import GObject import os from subprocess import Popen, PIPE import fcntl wnd = Gtk.Window() wnd.set_default_size(400, 400) wnd.connect("destroy", Gtk.main_quit) label = Gtk.Label() label.set_alignment(0, 0) wnd.add(label) wnd.show_all() sub_proc = Popen("ping -c 10 localhost", stdout=PIPE, shell=True) sub_outp = "" def non_block_read(output): ''' even in a thread, a normal read with block until the buffer is full ''' fd = output.fileno() fl = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) op = output.read() if op == None: return '' return op.decode('utf-8') def update_terminal(): label.set_text(label.get_text() + non_block_read(sub_proc.stdout)) return sub_proc.poll() is None GObject.timeout_add(100, update_terminal) Gtk.main()
source share