|
Textview updated with ouput from shell command: msg#00041gnome.gtk+.python
Hi, a quite frequent situation when writing pygtk apps is that of having to show the output generated by a shell command inside a textview. As that is a recurrent question I've been trying to write a simple example so it could be put in the FAQ. The problem seems to be avoiding blocking in the external process, so the example in FAQ 23.20, with a coroutine approach calling gtk.main_iteration() doesn't work. Most people seem to be using threads to do the job, but apparently it is also possible to use pipes in *nix systems in non-blocking mode. So far I could only get working a threaded example and my question is... can you think of a portable solution (*nix, Win32 at least...) that can be used as easily as the threaded one but without using threads?. Could be fine using a threaded solution here?. Are you fine with an example like this going into the FAQ? Any comments to improve the example so it can go to the FAQ? --- Example #!/usr/bin/env python import os, threading, locale import pygtk pygtk.require('2.0') import gtk import gobject encoding = locale.getlocale()[1] utf8conv = lambda x : unicode(x, encoding).encode('utf8') sw = gtk.ScrolledWindow() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) textview = gtk.TextView() textbuffer = textview.get_buffer() sw.add(textview) def on_button_clicked(button, buffer): command = 'dir -R %s' % os.getcwd() thr = threading.Thread(target= read_output, args=(command, buffer)) thr.run() def read_output(oscommand, txtbuffer): stdin, stdouterr = os.popen4(oscommand) for line in stdouterr.readlines(): txtbuffer.insert(txtbuffer.get_end_iter(), utf8conv(line)) win = gtk.Window() win.resize(300,500) win.connect('delete-event', gtk.main_quit) button = gtk.Button(u"Press me!") button.connect("clicked", on_button_clicked, textbuffer) vbox = gtk.VBox() vbox.pack_start(button, gtk.FALSE) vbox.pack_start(sw) win.add(vbox) win.show_all() gtk.main() -- Take care, Pachi _______________________________________________ pygtk mailing list pygtk@xxxxxxxxxx http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/ |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: making text clickable: 00041, Chris Lambacher |
|---|---|
| Next by Date: | Tray icon: 00041, Paco Moreno |
| Previous by Thread: | making text clickablei: 00041, Saurabh Wagh |
| Next by Thread: | Tray icon: 00041, Paco Moreno |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |