Re: Call external programs and keep showing its outputs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Il 10/10/2013 06:55, Woody Wu ha scritto:
> Hi,
> 
> I am about to write a test runner with GTK+. It basically can list a lot
> of external test programs (non-GTK+ pure Linux apps) in a treeview and
> let user select one of them to run. While an external program is
> running, I want to get all its outputs (to stdout/stderr) and keep
> updating in a textview.
> 
> Since I am a Linux programmer, so I know how to fork a new process and
> pipe into an external program. But I don't know in GTK+, what's the
> right method to keep fetching the information from the pipe and showing
> them in my GUI widgets.  On the other hand, while an external program is
> running, I don't want user feel that the my program is frozing and user
> should be able to kill the running external program by clicking a 'stop'
> button on the GUI.
> 
> For this kind of task, what GTK+ technology I should go to grab?  Thanks
> in advance!
> 

Hope you like pyhton :)


-- 
Hofstadter's Law:
"It always takes longer than you expect, even when you take into account
Hofstadter's Law."

Attachment: test_pygtk.ui
Description: application/designer

# -*- coding: UTF-8 -*-

import gobject
import gtk
import subprocess

class load_ng:
    def __init__(self):
        ###
        # Initialize the data
        ###
        self.error = 0
	self.base = '0xB0'
        self.com = ["ls","-l"]
        self.log = open('test_pygtk.log', 'w')
        ###
        # Initialize the gui
        ###
        self.builder = gtk.Builder()
        self.builder.add_from_file("test_pygtk.ui")
        self.window = self.builder.get_object("window_main")
        # 
        text = self.builder.get_object("txt_console")
        self.txt_buf = text.get_buffer()
        self.ok_tag = self.txt_buf.create_tag("ok", foreground="lightgreen", weight=700)
        self.ko_tag = self.txt_buf.create_tag("ko", foreground="red", weight=700)
        self.err_tag = self.txt_buf.create_tag("err", foreground="red")
        
        # connect signals
        self.builder.connect_signals(self)

    def on_btn_check_clicked(self, widget):
        # resetto il flag di errore
        self.error = 0

        self.test_launch()        
        itr = self.txt_buf.get_end_iter()
        if self.error != 0:
            self.txt_buf.insert_with_tags(itr, "Unexpected error; please see \nlog file for futher details.\n", self.err_tag)
        else:
            self.txt_buf.insert(itr, "###\n")            

    def on_btn_quit_clicked(self, widget):
        self.log.close()
        gtk.main_quit()

    def on_window_main_destroy(self, widget, data=None):
        self.log.close()
        gtk.main_quit()

    def test_launch(self):
        # if you want, you can change command options here:
        self.com[1] = "-la"

        itr = self.txt_buf.get_end_iter()
        self.txt_buf.insert(itr, "Launching command...\t\t\t\t\t\t")
        while gtk.events_pending():
            gtk.main_iteration_do(False)

        risposta = subprocess.Popen(self.com, stdout=subprocess.PIPE).communicate()[0]
        lines = risposta.split('\n')

        stato = False
        for line in lines:
            # maybe a better test can be designed ;)
            if '.' not in line:
                stato = True

        if stato:
            self.txt_buf.insert_with_tags(itr, "[OK]\n", self.ok_tag)
            self.txt_buf.insert(itr, risposta)
        else:
            self.txt_buf.insert_with_tags(itr, "[Failed]\n", self.ko_tag)
            self.error = 1
            self.log.writelines(risposta)

    def main(self):
        self.window.show()
        gtk.main()
        
        pass

if __name__=='__main__':
    app = load_ng()
    app.window.show()
    gtk.main()

_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux