UPDATE:
I forgot about the '$mw->update;' commands that would need to go in there as well, so that the Cancel button press is seen. However, in the example script this is handled by the output sub.

Hello World!\n

Often when I'm creating Tk GUI based scripts, I include a Cancel button for the longer operations.

However, the only way I have found to make the cancel button work is to have many 'return if $cancel;' type commands interspersed all through the subroutine, which can get very tedious if the routine is quite long and complicated.

So, I was wondering, is there an easier way to get a subroutine to terminate?

Thanks,

Spike.

In the following simple example, as the loop is small, it's easy to include a 'return if $cancel;' command, but imagine that the start subroutine is a very long and complicated routine, without a simple loop structure. Then you've got to add 'return if $cancel;' over and over again.

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; }

In reply to One shot way to end a sub? by spikey_wan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.