## # see also a demonstration by zentara, particularly -textvariable # http://www.perlmonks.org/?node_id=1087787 # # my $msg : shared = "Start"; # $btn1 = $mw->Button(-textvariable => \$msg, ...); # later : $msg = "Step Two"; $btn1->update; # # thank you, zentara++ ## use strict; use warnings; use threads; use threads::shared; use Thread::Queue; use Tk; my $que = Thread::Queue->new(); my $msg : shared = "Start"; # detach the thread so not to get thread warnings # about running threads when exiting the app (my $thr = threads->create('bg_task'))->detach(); my $mw = MainWindow->new(); $mw->protocol( WM_DELETE_WINDOW => \&quit ); $mw->geometry("+150+100"); $mw->Label( -text => "Tk + threads Demo", -height => 2, -width => 22 )->pack; my $btn1 = $mw->Button( -text => $msg, -command => \&fun, -width => 10 ); $btn1->pack(); my $btn2 = $mw->Button( -text => "Quit", -command => \&quit, -width => 10 ); $btn2->pack(); my $timer = $mw->repeat( 100, sub { $btn1->configure( -text => $msg ); }); MainLoop(); sub fun { $que->enqueue("some event"); return; } sub quit { $timer->cancel; # for threads, is helpful to signal the app to # terminate so not to segfault on some platforms kill('TERM', -$$); exit; } sub bg_task { while ( my $event = $que->dequeue ) { $msg = "Step One"; sleep 1; $msg = "Step Two"; sleep 1; $msg = "Step Three"; } }