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


In reply to Re^4: Tk how-to? by rcseege
in thread Tk how-to? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.