in reply to Re^4: Dynamically Filling Text Box from Perl Tk
in thread Dynamically Filling Text Box from Perl Tk
Also depending on what the module product_search does, it MAY render the Tk GUI's eventloop screwed up. You might want to make product_search operate in a separate thread, or put it in a standalone script, or run it thru IPC::Open3 or a piped open and collect the results back thru Tk's fileevent. Then you can decide when you want to write these results to the text widget, with an $txt->insert( 'end', $result );
Here is a simple example showing the concepts I described.
#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(-background => 'gray50'); my $text = $mw->Scrolled('Text', -bg=>'white' )->pack(); my $pid; my $startb = $mw->Button( -text => 'Start', -command=> \&work, )->pack(); my $count = 0; my $label = $mw->Label(-textvariable=>\$count)->pack(); my $testtimer = $mw->repeat(500, sub { $count++} ); my $stopb = $mw->Button( -text => 'Exit', -command=>sub{ kill 9,$pid; exit; }, )->pack(); MainLoop; ##################################### sub work{ $startb->configure(-state=>'disabled'); use Fcntl; + my $flags; + # 3 second delay between outputs $pid = open (my $fh, "top -b -d 3 |" ) or warn "$!\n"; fcntl($fh, F_SETFL, O_NONBLOCK) || die "$!\n"; # Set the non-block f +lags my $repeater; $repeater = $mw->repeat(10, sub { if(my $bytes = sysread( $fh, my $buf, 1024)){; $text->insert('end',$buf); $text->see('end'); } } ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Dynamically Filling Text Box from Perl Tk
by amdme127 (Novice) on Nov 08, 2011 at 15:29 UTC |