Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Tk how-to?

by converter (Priest)
on Dec 04, 2005 at 14:17 UTC ( #513953=note: print w/ replies, xml ) Need Help??


in reply to Tk how-to?

I have a nice collection of Tk-related links on my home node. I'm sure you'll find more than enough there to get you started down the road of Tk madness.


Comment on Re: Tk how-to?
Re^2: Tk how-to?
by BrowserUk (Pope) on Dec 04, 2005 at 15:01 UTC

    Nice collection of links. You don't know of one that describes how to bind to a Canvas redraw event do you?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      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"

        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).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-08-30 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls