use Tk; use Tk::ProgressBar; use strict; use warnings; my $count; my $cancel; my $mw = new MainWindow(-title => "demo"); $mw->Button(-command => sub {do_something(\$count, \$cancel)}, -text => "Do Something")->pack(); $mw->Button(-command => sub {cancel(\$cancel)}, -text => "Cancel")->pack(); my $pb = $mw->ProgressBar( -from => 0, -to => 100, -blocks => 100, -colors => [0, 'green', 50, 'yellow' , 80, 'red'], -variable => \$count )->pack(); MainLoop; sub do_something { my ($count_r, $cancel_r) = @_; $$count_r = 0; $$cancel_r = 0; while (!$$cancel_r & $$count_r < 100) { $$count_r ++; $mw->update(); sleep(1); } } sub cancel { my $cancel_r = shift; $$cancel_r = 1; }