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

Re: updating gui while running a subroutine



On Fri, 30 Jan 2009 12:51:44 -0400
Daniel Gaston <daniel gaston gmail com> wrote:

>Hi folks, looking for a little assistance, and I'm sure it is just something
>simple that I can do to fix this. Using the information from an earlier
>thread I have it so when my GUI is running an external program a text-view
>that I have for monitoring progress updates. I've done this as Muppet
>recommended using the following:

>I want to call this first subroutine in a similar manner to above so that
>the GUI will still update with progress messages as I do in the above
>example.
>
>Is there something special I need to do with the piped open/fork to have it
>work for a subroutine within the same program code?
>
>Thanks.
>
>Dan Gaston

Hi, there are many ways to do what you want, but here is a simple way,
using the Gtk::Helper instead of IO::Watch.
I repeatedly run "date" every 3 seconds as the spawned command, but you
could work out something more elaborate.

I may not be understanding the complexity of your question, so test,test ,test. :-)

#!/usr/bin/perl -w
use strict;
use Glib  qw(TRUE FALSE);
use Gtk2 -init;
use Gtk2::Helper;
use IO::Pipe;

#standard window creation, placement, and signal connecting
my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { exit;});
$window->set_border_width(5);
$window->set_position('center_always');

my $hbox = Gtk2::HBox->new();
$hbox->set( "border_width" => 0 );
$window->add($hbox);

my $scroll   = Gtk2::ScrolledWindow->new;
$scroll->set_size_request(600,600);
my $textview = Gtk2::TextView->new;
my $buffer = $textview->get_buffer;
create_tags($buffer);

$scroll->add($textview);
$hbox->pack_start($scroll,1, 1, 1 ); # expand?, fill?, padding;

$window->show_all();

my $helper_tag;

my $timer = Glib::Timeout->add (3000, \&launch );

#our main event-loop
Gtk2->main;

sub launch{
 #open a pipe to a command
 my $fh = new IO::Pipe;
 $fh->reader("date");
 #$fh->reader("counter1.pl");

#add a watch to this file handle
$helper_tag = Gtk2::Helper->add_watch(fileno $fh, 'in',sub{
			&watch_callback($fh);
	    });
return 1;  #keep timer going
}

sub watch_callback {

	my ($fh) = @_;
	
	my $line;
	#read 1000 caracters of the buffer
	$fh->sysread($line,1000);
	#remove the newline
#	print $line;
	chomp $line;
	
	if($line){
           $buffer->insert_with_tags_by_name($buffer->get_end_iter, "$line\n",'blue');
	}else{
           Gtk2::Helper->remove_watch($helper_tag);				 
	}

        
	#absolutely need the main iteration here, or you
	#won't get the end iter correctly 
        Gtk2->main_iteration while Gtk2->events_pending;
        $textview->scroll_to_iter($buffer->get_end_iter,0,0,0,0);

#        my $end_mark = $buffer->create_mark( 'end', $buffer->get_end_iter, FALSE );
#        $textview->scroll_to_mark( $end_mark, 0.0, FALSE, 0.0, 1.0 );

	#important so we can loop again	
	return 1;
}
###########################3
sub create_tags{
  my $buffer = shift; 

   $buffer->create_tag('blue',
           foreground => 'blue',
           );

   $buffer->create_tag('col',
           foreground => 'green',
	   );

}
##############
__END__



Hope it shows you a simple way. Gtk::Helper makes filehandle watching
a bit easier.

Goodluck,
zentara


-- 
I'm not really a human, but I play one on earth.
http://www.zentara.net/~zentaran/Remember_How_Lucky_You_Are.html 


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