use strict; use warnings; use Tk; use Tk::ROText; my $cancel = 0; my $mw = MainWindow -> new (-title => " loop test"); $mw -> withdraw; $mw -> minsize (qw(700 400)); my $status = $mw -> Scrolled ("ROText", -scrollbars => 'e', -background => 'white', ) ->pack( -expand => 1, -fill => 'both', ); $status -> configure(-wrap => 'word'); my $exit = $mw -> Button ( -text , 'Exit', -command, \&my_exit, ) -> pack (-side, 'left'); my $start = $mw -> Button ( -text , 'Start', -command, \&start, ) -> pack (-side, 'right'); $mw -> Popup; $mw -> focus; MainLoop(); sub start { $cancel = 0; $start -> configure ( -state => 'disabled', -relief => 'sunken'); $exit -> configure ( -text => 'Cancel', -command => \&cancel); for (1..100) { # last if $cancel; output ("$_\n"); sleep 2; } done(); } sub cancel { output ("Cancelling...\n"); $cancel =1; } sub my_exit { exit(); } sub done { $start -> configure ( -state => 'normal', -relief => 'raised'); $exit -> configure ( -text => "Exit", -command => \&my_exit); output ("Done.\n"); } sub output { my $text = $_[0]; $status->insert('end', "$text"); $status -> see ('end'); $mw->update; }