Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Tk how-to?

by liverpole (Monsignor)
on Dec 04, 2005 at 17:22 UTC ( [id://513971]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Tk how-to?
in thread Tk how-to?

Greetings BrowserUk!

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;

@ARGV=split//,"/:L"; map{print substr crypt($_,ord pop),2,3}qw"PerlyouC READPIPE provides"

Replies are listed 'Best First'.
Re^4: Tk how-to?
by rcseege (Pilgrim) on Dec 04, 2005 at 17:38 UTC

    This is good stuff, and you raise a really good point: the bind method for Canvas is overridden to be used for Canvas items. This is a pretty nasty and non-intuitive behavior, IMO. It would have been better if there had been itemBind method or something similar, but anyway...

    It used to be that in order to get the original implementation of bind, one had to use $canvas->Tk::bind(...), which I continue to use. As you say, CanvasBind is provided (It is basically an alias for Tk::bind), and is barely documented in the docs I have.

    Configure can be used as you suggest, but I'd suggest that Expose be used instead. I consider Configure to be the "big gun" in Tk when I need to react to lots of different changes in one event handler, but tend to avoid it when I can, since it gets called a lot. In this case, I think Expose will suffice -- fewer things trigger it. A quick snippet:

    use Tk; my $mw = MainWindow->new; my $canvas = $mw->Canvas-> pack(-expand => 1, -fill => 'both'); $canvas->Tk::bind('<Expose>', sub { my $c = shift; print $c->width . " x " . $c->height . "\n"; }); MainLoop;
    Rob
    Update: I'll back off from my statement on Configure vs Expose a bit, and instead recommend that you test with both, and use one or the other. Depending on what you're doing, Configure may be the way to go. There are times when an Expose event will be called but a Configure will not. While running the above snippet, move another window in front of the Tk window, then move it away. Expose will be triggered, where Configure will not, so there's one case at least.

    The point is that both get called upon resize - once the rest of your app is coded, you might do some basic testing to determine which binding is triggered more often for events other than resize. You could find that the difference is negligible.

    An interesting note is that although ResizeRequest is listed in the Tk bind docs, it isn't supported within Tk. It's support was removed in Tk 4, because it's usefulness was questionable and it ended up causing confusion. It was triggered before the resize had happened and not after (like Configure or Expose).

      Thanks for the useful feedback, rcseege.  I was going to update with the fact that Tk::bind was another option, but got pulled away to something else, so I'm glad you mentioned it.

      I will also point out, for those without access to Mastering Perl/Tk, that there is documentation on this at ActiveState's site; for which you can search in this page for the string $canvas->bind.

      Good information about Configure vs. Expose++.  This is the main reason I like coming to Perlmonks so much.


      @ARGV=split//,"/:L"; map{print substr crypt($_,ord pop),2,3}qw"PerlyouC READPIPE provides"
        there is documentation on this at ActiveState's site; for which you can search in this page for the string $canvas->bind.

        I certainly agree. Plus if you've downloaded ActivePerl, this same documentation is installed, which is very handy. That brief mention in the Canvas docs was what I meant by "barely documented". What I was getting at was that the CanvasBind method should have been better documented than it was.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://513971]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found