Re: how to run a command-line program from Perl/Tk (ExecuteCommand?)



Will Parsons wrote:
Ch Lamprecht wrote:

Will Parsons wrote:

zentara wrote:



I'm still struggling to get this to work on Windows. I've tried to adapt the 'queued solution' example in the link above for my purpose,
<snip>
(The script being run is non-interactive but takes time to complete - I simply want to see its output as it's produced in a ROText widget.)

Any help, whether to fix the above approach, or to suggest a different one would be appreciated.


Hi,

If you want to use Tk with threads, you have to create the threads before you load the Tk libs:


I don't see how I can to do that - I can't create the thread until it is
requested in response to some user input, so I must already have Tk loaded.

You can create a thread and let it sleep until it is needed.

(Actually, I don't *want* to use threads anyway - if I had a usable alternative, I wouldn't.)

If it's ok having the UI block for the time of your commands execution, you can do it like that:

Christoph

use warnings;
use strict;

use Tk;
my $window = new MainWindow;
my $text = $window->Scrolled('ROText', -width => 90, -height => 24)
->pack(-fill => 'both', -expand => 1);

my $btn = $window->Button(-command => \&start_process,
-text => 'start_process')->pack;
MainLoop();


sub start_process{
$btn->configure(-state => 'disabled');
eval process();
$btn->configure(-state => 'normal');
}
sub process{
my $reader;
$text->insert('end', "opening pipe:\n");
open $reader, 'perl test.pl |' or die "can't open reader: $!";
while (my $line = <$reader>) {
$text->insert('end',$line);
$text->see('end');
$window->update;
}
}


with test.pl:
$| = 1;
for (o..9){
print "hello world $_\n";
sleep 1;
}



--
use Tk;use Tk::GraphItems;$c=tkinit->Canvas->pack;push@i,Tk::GraphItems->
TextBox(text=>$_,canvas=>$c,x=>$x+=70,y=>100)for(Just=>another=>Perl=>Hacker);
Tk::GraphItems->Connector(source=>$i[$_],target=>$i[$_+1])for(0..2);
$c->repeat(30,sub{$_->move(0,4*cos($d+=3.16))for(@i)});MainLoop
.