in reply to how to fork & join in tk?
Using Tk::ExecuteCommand, you can change the text of the ExecuteCommand module's default buttons to anything you want.
#!/usr/bin/perl -w use Tk; use Tk::ExecuteCommand; use Tk::widgets qw/LabEntry/; use strict; my $mw = MainWindow->new; my $ec = $mw->ExecuteCommand( -command => '', -entryWidth => 50, -height => 10, -label => '', -text => 'Execute', )->pack; $ec->configure(-command => 'date; sleep 10; date'); my $button = $mw->Button(-text =>'Do_it', -background =>'hotpink', -command => sub{ $ec->execute_command }, )->pack; MainLoop;
Or you may want to try a thread with a shared variable, you can pass strings to be eval'd in the thread $data_in. I just used numbers in the simple hack.
#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; # declare, share then assign my $data_in:shared = 0; my $data_ret:shared = '----'; my $button_control:shared = 0; my $go_control:shared = 0; my $die_control:shared = 0; #create thread before any tk code is called my $thr = threads->create( \&worker ); use Tk; my $mw = MainWindow->new(); my $val = '----'; my $label = $mw->Label( -width => 50, -textvariable => \$val )->pack(); my $button = $mw->Button( -text => 'Start', -command => \&start )->pack(); # you need a timer to read the shared var in thread my $timer = $mw->repeat(10 , sub{ $val = $data_ret; }); my $timer1 = $mw->repeat(10, sub{ if($button_control){ $button ->configure(-text=> " +Next"); $button_control = 0; } }); MainLoop; sub start{ $button ->configure(-text=> "----"); $go_control = 1; $data_in = int rand(20); # pass some new data in } # no Tk code in thread sub worker { my $count; while(1){ if($die_control){ print "thread finishing\n"; return} #wait for $go_control if($go_control){ if($die_control){ print "thread finishing\n"; return} print "incoming $data_in\n"; $count++; print $count,"\n"; if($count >= $data_in){ $go_control = 0; $data_ret = $count +; $button_control = 1;} $data_ret = $count; select(undef,undef,undef,.25); }else{ $count = 0; select(undef,undef,undef,.25); }# sleep until awakened } return; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to fork & join in tk?
by marioroy (Prior) on Apr 23, 2017 at 19:28 UTC | |
by zentara (Cardinal) on Apr 24, 2017 at 16:03 UTC | |
by marioroy (Prior) on Apr 24, 2017 at 18:42 UTC |