in reply to Re^2: Tk how-to?
in thread Tk how-to?
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Tk how-to?
by rcseege (Pilgrim) on Dec 04, 2005 at 17:38 UTC | |
by liverpole (Monsignor) on Dec 04, 2005 at 19:04 UTC | |
by rcseege (Pilgrim) on Dec 04, 2005 at 19:12 UTC |