I just recently came across the infamous Canvas bind problem again.
According to Mastering Perl/Tk (published by O'Reilly), you need to use a different bind method with a Canvas object, called CanvasBind. (This is described in chapter 9.4, and it's because a Canvas object has its own bind method).
Here's an example program I created to demonstrate. When you resize the main window (and thus the Canvas object), it draws random squares as a result of the change in size and/or position (the event for which is a "<Configure>"):
#!/usr/bin/perl -w + # Strict use strict; use warnings; + # Libraries use Tk; + # Subroutines sub random($) { int rand $_[0] } sub random_square($) { my $can = shift; my ($x0, $y0, $x1, $y1) = (random 256, random 256, random 256, ran +dom 256); $x1 += $x0; $y1 += $y0; my $bg = sprintf "#%02x%02x%02x", random 256, random 256, random 2 +56; $can->createRectangle($x0, $y0, $x1, $y1, -fill => $bg); } # Main program my $mw = new MainWindow(-title => "Canvas binding test"); my $can = $mw->Canvas(-bg => 'white', -width => 512, -height => 512); $can->pack(-expand => 1, -fill => 'both'); $can->CanvasBind('<Configure>', [\&random_square]); MainLoop;
In reply to Re^3: Tk how-to?
by liverpole
in thread Tk how-to?
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |