Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a long TK program which I cannot include here. It precesses data and outputs results to a text widget. I am having a problem when cancelling my top level window, especially if the input data file is too long. The data processing keeps executing even though I had withdrawn the top level widget. Is this a common thing in Perl/Tk? Here is an abbreviation of my code:
$tl = $mw->Toplevel(-title=>"Stupid Program"); while (<DATA>){ process data output to a text widget } my $cancel_btn=$action_frame->Button(-text=>"Cancel", -command=> sub { $tl->withdraw })->pack(-side =>'left', -expand=>1, -anchor => 'cente +r');
I have to resort to killing the application in order to stop the process. Any ideas what I can do to resolve this issue? Thank you O great Saints!

Replies are listed 'Best First'.
Re: $widget->withdraw
by zentara (Cardinal) on Jun 02, 2006 at 16:41 UTC
    It's a common mistake in Tk, to use while loops, or sleep statements. These block the gui. Your main misunderstanding, is that you think that withdraw destroys a widget and all it contains. Withdraw just removes it from being mapped onto the screen, it still exists behind the scenes and can be running. The following code is a quick fix
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = tkinit; my $tl = $mw->Toplevel(-title=>"Stupid Program"); my $text = $tl->Text()->pack(); my $flag = 0; my $cancel_btn= $tl->Button(-text=>"Cancel", -command=> sub { $flag = 1; $tl->withdraw })->pack(-side =>'left', -expand=>1, -anchor => 'center'); + my $do_btn= $tl->Button(-text=>"Start", -command=> sub { while (<DATA>){ # process data # output to a text widget if($flag){last} $text->insert('end',"$_\n"); $text->update; $mw->after(1000); } })->pack(-side =>'left', -expand=>1, -anchor => 'center'); + MainLoop; __DATA__ 1 2 3 4 5 6 7 8 9 10
    You could also do it without a flag, using the ismapped method.

    I'm not really a human, but I play one on earth. flash japh
      Zentara, You are a Saint! Thanks for the clarification. I would apply the ismapped function.
        No, I'm not a Saint.......just a happy Abbot. ;-)

        I'm not really a human, but I play one on earth. flash japh