Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

G'day All,

Background: I've been playing around in Tk, looking at the feasibility of using Tk::Canvas to generate some simple, interactive maps for use with RPGs. I came across the createGroup() method, which would help enormously with abstraction and code reduction; however, I'm unable to find any substantial documentation for this.

I found this method in "Mastering Perl/Tk" (Chapter 9: The Canvas Widget; The Group Item; pp. 212-213). Over half of that is taken up with a couple of screenshots. There's a minimal code example, with very little explanation, which looks like:

$one = $canvas->createOval(...); $two = $canvas->createRectangle(...); $group = $canvas->createGroup([0, 0], -members => [$one, $two]); $mw->update; $mw->after(1000); $canvas->move($group, 100, 100);

I've no idea what the [0, 0] is supposed to do. My first thought was that it was related to placement; but, after putting all sorts of different numbers in there, I see no change. It's also unclear why the update() is necessary — but it is.

I've checked for documentation, or other references, in the following places:

  • The Tk::Canvas documentation has no mention of createGroup().
  • The Github repo (https://github.com/eserte/perl-tk/blob/master/Canvas/Canvas.pm) does show group, along with other create methods (arc, oval, window, etc.) but there's no code for me to study (looks like it's probably in XS-code).
  • "Active bugs for Tk" has no mention of missing documentation.
  • An internet search was not helpful: mostly links to the online documentation I'd already looked at, and the online pirated versions of "Mastering Perl/Tk".

I put together the following test script. This was mainly to check that what was written in the book was valid.

#!/usr/bin/env perl use strict; use warnings; use Tk; my $mw = MainWindow::->new(); my $scrl_can = $mw->Scrolled('Canvas', -scrollregion => [0, 0, 300, 300], )->pack(qw{-fill both -expand 1}); my $c = $scrl_can->Subwidget('canvas'); for my $x (0, 100, 200) { my $grassy_creek = $c->createGroup( [0,0], -members => [grass($c), creek($c)] ); $mw->update(); $c->move($grassy_creek, $x, 100); } MainLoop; sub grass { return $_[0]->createRectangle( 0, 0, 100, 100, -fill => '#009900', -outline => undef ); } sub creek { return $_[0]->createRectangle( 0, 30, 100, 70, -fill => '#0000cc', -outline => undef ); }

That worked: I got (using a modicum of imagination) a creek running through a grassy area (three blocks wide). That's the type of abstraction I was looking for but I still wasn't happy with the cargo-cult programming.

I wondered if the ARRAYREF ([0, 0]) had something to do with the number of members in the group. I put a bend in the last part of the creek using a group with four members; this worked, still with [0, 0]. Here's the important part of the code for that test:

my $grassy_creek_bend = $c->createGroup( [0,0], -members => [ grass($c), creek_half_w($c), creek_half_s($c), creek_bend_ws($c), ] );

The full code is somewhat lengthy; it's in the following spoiler:

I'm using Perl 5.32.0 and Tk 804.035.

So, this leads to the questions. Can anyone point me to any documentation about this? In the absence of doco, does anyone have an explanation for the [0, 0] and the need for the update()?

Thanks in advance.

— Ken


In reply to Tk::Canvas createGroup() Undocumented by kcott

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (6)
    As of 2021-05-07 19:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Perl 7 will be out ...





      Results (93 votes). Check out past polls.

      Notices?