My goal is to compute and set the color of every pixel in a 500x500 image. Placing the entire computation in a single call back leaves my machine unresponsive for fifteen to thirty seconds. The only solution that I could think of was to compute one row of pixels at a time. Here is a demo of my implementation:
use strict; use warnings; use v5.14; use Tk; my $mw = new MainWindow( -title => 'Event demo'); my $drawarea = $mw->Frame()->pack( -side => 'top', -fill => 'both' ); $mw->bind('<<RowDone>>' => \&next_row); my $p = $mw->Photo(-width=>500, -height=>500); my $canvas = $drawarea->Canvas( -relief => 'ridge', -width => 500, -height => 500, -borderwidth => 4 )->pack(); $canvas->bind('<<RowDone>>' => \&next_row); $canvas->createImage(0,0, -anchor=>'nw', image=>$p); my $plot = $mw->Button(-text=>'Plot', -command=>\&init) ->pack(-side=>'left'); MainLoop; # Callbacks sub init { our $y = 0; $mw->eventGenerate('<<RowDone>>'); return; } sub next_row { our $y; 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' ); } return; } # stubs for demo only sub color { return 'red' } sub long_computation { return 10; }
This works as intended, but brings up some new issues.
The current row number ($y) must be declared globally with 'our'. I would prefer to pass a lexical value from one iteration to the next through the event structure. I have not been able to find a way to do this.
There does not seem to be any way to cancel a calculation in progress. I am unable to cancel a <<RowDone>> event which is already scheduled (or soon will be).
This is very likely an X-Y problem. I am interested in better solutions to the original problem as well as improvements to my solution.
In reply to long computation in TK by BillKSmith
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |