You don't need to make $y global, it's enough to share it among the subroutines that need it. I made them close over the $y, but you can also abstract the whole logic into a class where y is the attribute.

I don't know whether it's possible to create custom events with any event structure you'd like to define, but I fear it's not.

I'm not sure whether an event can be cancelled, but you can definitely not schedule the following one, and do nothing in the current one.

#! /usr/bin/perl use strict; use warnings; use Time::HiRes qw{ usleep }; use Tk; my $mw = 'MainWindow'->new(-title => 'Event demo'); $mw->bind('<<RowDone>>' => \&next_row); my $drawarea = $mw->Frame->pack(-side => 'top', -fill => 'both'); my $p = $mw->Photo(-width => 500, -height => 500); my $canvas = $drawarea->Canvas( -relief => 'ridge', -width => 500, -height => 500, -borderwidth => 4 )->pack; $canvas->createImage(0, 0, -anchor => 'nw', image => $p); $mw->Button(-text => 'Plot', -command => \&init) ->pack(-side => 'left'); $mw->Button(-text => 'Stop', -command => \&stop) ->pack(-side => 'left'); $mw->Button(-text => 'Quit', -command => sub { Tk::exit() }) ->pack(-side => 'left'); MainLoop(); { my ($y, $done); sub init { undef $done; $y = 0 if ! defined $y || $y == 500; $mw->eventGenerate('<<RowDone>>'); } sub next_row { return if $done; for my $x ( 0..499 ) { my $quality = long_computation($x, $y); $p->put(color($quality), '-to', $x, $y); } $canvas->update; if (++$y < 500) { $mw->eventGenerate('<<RowDone>>', -when => 'tail' ); } } sub stop { $done = 1 } } sub color { qw( red blue )[ rand 2 ] } sub long_computation { usleep(100) }

*Update*: return from next_row at the very beginning.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

In reply to Re: long computation in TK by choroba
in thread long computation in TK by BillKSmith

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.