#!/usr/local/bin/perl use strict; use Tk; my $Mycount = 0; my $submain = MainWindow->new(); my $buttons = $submain->Frame()->pack(-side => 'top'); my $status = $buttons->Button(-text => 'Click to Clear', -command => sub {$submain->destroy;} )->pack(-side=>'left'); my $text1 = $submain->Text ('-width'=>100, 'height'=>20); $text1->pack(-side => 'left', -fill => 'y'); my $doit = $buttons->Button(-text => 'Do It', -command => [ \&theLoop => $submain, \$Mycount], )->pack(-side=>'left'); $text1->insert('end', "start\n"); $text1->pack; MainLoop(); sub theLoop { my $widget = shift; my $Mycount = shift; my $line = ''; $$Mycount++; #for(my $loop=0; $loop <= 3; $loop++ ) ## Looping handled by timer... #{ #sleep 1; Incompatible with Tk MainLoop - It pauses the *entire* application print "$$Mycount\n"; if ( $$Mycount == 1 ) { $line = "$$Mycount case 1\n"; } elsif ( $$Mycount == 2 ) { $line = "$$Mycount case 2\n"; } else { $line = "$$Mycount something else\n"; } $text1->insert('end', "$line"); #$text1->pack; #} ## Here is where we implement the delay.... and the loop ## We simply check to see if our loop max has been reached, then if not, ## set a future event to call this subroutine again. ## The 500 is the number of milliseconds to wait before calling. ## This is better programming in Tk, since the sleep command stops everything ## which is very bad with a GUI. (think M$ when a low level call happens) if ($$Mycount < 3) { my $delay = $submain->after(500, [\&theLoop => $widget, $Mycount]); } }