Ok! I have tested this, and it only works at the end of the execution of the command, I attach the program to show this, please, execute with: # ./textview-basic.py
The thing is that output is shown when command has finished executing, not while executing, so no progress is shown. This is a heavily modified version of an example. When in the application, please, press "Format" That executes the script "prueba.sh" (which means test.sh in Spanish). It is safe, you can check it, it doesn't format anything, yet!! Make both files executable, of course. Thanks!! 2008/10/27 A.T.Hofkamp <[EMAIL PROTECTED]>: > Néstor Amigo Cairo wrote: >> >> Hi! >> >> I'm working in a GUI application as a frontend for some commands to >> copy some files into a Compact Flash. My question is: how can I >> execute the command from the GUI and show the output on a TextView >> inside the GUI application, line by line?? (the command is 'badblocks > > In short: > > launch the command, and feed the output handle into > > gobject.io_add_watch() > > you will get an event when something interesting (like arrival of new text) > happens. > > In the handler, read the data, and add it to your textview widget. > > > Albert > -- Néstor +34 687 96 74 81 [EMAIL PROTECTED]
#!/usr/bin/env python
# example textview-basic.py
import pygtk
pygtk.require('2.0')
import gtk
import dbus
import threading
import time
import subprocess
import gobject
gtk.threads_init()
"""
class Test(threading.Thread):
stopthread = threading.Event()
def initialize(self, stream, textbuffer):
self.stream = stream
self.textbuffer = textbuffer
def run(self):
system_bus = dbus.SystemBus()
for line in self.stream:
self.textbuffer.set_text(line)
print line
def stop(self):
#Stop method, sets the event to terminate the thread's main loop
self.stopthread.set()
"""
class TextViewExample:
def do_copy(self, widget, textbuffer):
output = commands.getoutput('/bin/ls')
textbuffer.set_text(output)
md5sum_status, md5sum_output = commands.getstatusoutput('md5sum -c MD5SUMS')
textbuffer.insert(textbuffer.get_end_iter(), str(md5sum_status))
def do_format(self, widget, textbuffer):
proc = subprocess.Popen(("./prueba.sh"),
shell=False,
stdout=subprocess.PIPE,
)
gobject.io_add_watch(proc.stdout, gobject.IO_IN, textbuffer.insert(textbuffer.get_end_iter(),proc.stdout.readline()))
"""
background = Test()
background.initialize(proc.stdout, textbuffer)
background.start()
"""
"""md5sum_status, md5sum_output = commands.getstatusoutput('md5sum -c MD5SUMS')
textbuffer.insert(textbuffer.get_end_iter(), str(md5sum_status))"""
def close_application(self, widget):
"""main_quit function, it stops the thread and the gtk's main loop"""
#Importing the fs object from the global scope
self.background.stop()
gtk.main_quit()
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_resizable(True)
window.connect("destroy", self.close_application)
window.set_title("TextView Widget Basic Example")
window.set_border_width(0)
box1 = gtk.VBox(False, 0)
window.add(box1)
box1.show()
box2 = gtk.VBox(False, 10)
box2.set_border_width(10)
box1.pack_start(box2, True, True, 0)
box2.show()
sw = gtk.ScrolledWindow()
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
textview = gtk.TextView()
textbuffer = textview.get_buffer()
sw.add(textview)
sw.show()
textview.set_editable(0)
textview.set_cursor_visible(0)
textview.show()
box2.pack_start(sw)
hbox = gtk.HButtonBox()
box2.pack_start(hbox, False, False, 0)
hbox.show()
vbox = gtk.VBox()
vbox.show()
hbox.pack_start(vbox, False, False, 0)
# check button to toggle editable mode
copyButton = gtk.Button("Copy")
vbox.pack_start(copyButton, False, False, 0)
copyButton.connect("clicked", self.do_copy, textbuffer)
copyButton.show()
# radio buttons to specify wrap mode
vbox = gtk.VBox()
vbox.show()
hbox.pack_start(vbox, False, False, 0)
# check button to toggle editable mode
formatButton = gtk.Button("Format")
vbox.pack_start(formatButton, False, False, 0)
formatButton.connect("clicked", self.do_format, textbuffer)
formatButton.show()
# radio buttons to specify wrap mode
vbox = gtk.VBox()
vbox.show()
hbox.pack_start(vbox, False, False, 0)
optionsButton = gtk.Button("Options")
vbox.pack_start(optionsButton, False, True, 0)
#radio.connect("toggled", self.new_wrap_mode, textview, gtk.WRAP_NONE)
optionsButton.show()
# radio buttons to specify justification
vbox = gtk.VBox()
vbox.show()
hbox.pack_start(vbox, False, False, 0)
upgradeButton = gtk.Button("Upgrade")
vbox.pack_start(upgradeButton, False, True, 0)
#radio.connect("toggled", self.new_justification, textview,
# gtk.JUSTIFY_LEFT)
upgradeButton.show()
separator = gtk.HSeparator()
box1.pack_start(separator, False, True, 0)
separator.show()
box2 = gtk.VBox(False, 10)
box2.set_border_width(10)
box1.pack_start(box2, False, True, 0)
box2.show()
button = gtk.Button("close")
button.connect("clicked", self.close_application)
box2.pack_start(button, True, True, 0)
button.set_flags(gtk.CAN_DEFAULT)
button.grab_default()
button.show()
window.show()
def main():
gtk.main()
return 0
if __name__ == "__main__":
TextViewExample()
main()
prueba.sh
Description: Bourne shell script
_______________________________________________ pygtk mailing list [email protected] http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/
