Dear monks, I have started writing a music program in which I have to create a number of repetitive elements. In the attached example, I want to produce a range of colours for each musical degree. The button labeled "CLR" is used to call the clrChooser subroutin, and the button labeled "CHK" is used to check the current value. It seems like the iteration is "overflowing". The for loop still has the correct values, but the subroutine has the maximum number + 1. Try reducing the iteration by one (or more) and see the result. Sorry for asking such a lame question here, this must be very beginner level, but I can't ask for advice elsewhere. Thank you, Zsolt

#!/usr/bin/perl -w use utf8; use Tk; require Tk::LabFrame; our ($degreeClr1, $degreeClr2, $degreeClr3, $degreeClr4, $degreeClr5) += ("magenta", "red", "blue", "green", "darkred"); my $mw = MainWindow -> new(); $mw -> title($0); $mw -> geometry("1100x150+0+0"); $mw -> minsize(1100, 150); # MAIN FRAME $frame01 = $mw -> Frame -> pack(-fill => 'both', -expand => 1, -padx = +> 4, -pady => 4); $frameParam1 = $frame01 -> LabFrame(-label => "Settings", -labelside = +> "acrosstop") -> pack(-fill => 'x', -expand => 1, -padx => 10, -anch +or => 'n'); $frameParam1_0 = $frameParam1 -> Frame -> pack(-fill => 'x', -expand = +> 1); $frameParam1_0 -> Label(-text => "") -> pack(-fill => 'x', -expand => +1, -side => 'left'); # FRAME COLORS $frameParam1_3 = $frameParam1 -> Frame -> pack(-fill => 'x', -expand = +> 1); $frameParam1_3 -> Label(-text => "Colors:") -> pack(-side => 'left'); for (my $i = 1; $i <= 5; $i++) { ${"frameParam1_3_$i"} = $frameParam1_3 -> Frame -> pack(-padx => 5, +-side => 'left'); ${"frameParam1_3_$i"} -> Label(-text => "$i. degree") -> pack(-side +=> 'left'); ${"clrButton$i"} = ${"frameParam1_3_$i"} -> Button(-text => "CLR", - +cursor => 'hand2', -command => sub{&clrChooser("Choose color for $i d +egree!", ${"degreeClr$i"}, ${"clrCanvas$i"}, ${"clrShow$i"}, ${"clrBu +tton$i"}, \${"degreeClr$i"})}) -> pack(-side => 'left'); ${"clrCanvas$i"} = ${"frameParam1_3_$i"} -> Canvas(-background => "b +lack", -width => 20, -height => 20, -state => 'disabled') -> pack(-si +de => 'left'); ${"clrShow$i"} = ${"clrCanvas$i"} -> createRectangle(20, 20, 4, 4, - +fill => ${"degreeClr$i"}, -outline => 'white', -width => '2'); ${"frameParam1_3_$i"} -> Button(-text => 'CHK', -command => sub{prin +t "degreeClr$i: " . ${"degreeClr$i"} . "\n"})-> pack(-side => 'left') +; print "degreeClr$i: " . ${"degreeClr$i"} . "\n"; } MainLoop; sub clrChooser { my ($title, $initClr, $clrCanvas, $clrCanvasItem, $clrButton, $initC +lrRef) = @_; print "title: $title\ninitClr: $initClr\nclrCanvas: $clrCanvas\ninit +ClrRef: $initClrRef\n"; my $color = $mw -> chooseColor(-title => $title, -initialcolor => $i +nitClr); if ($color) { $$clrCanvas -> itemconfigure(${$clrCanvasItem}, -fill => $color); ${$initClrRef} = $color; $clrButton -> focus; } }

In reply to Strange behavior of iteration while creating Perl/Tk widgets dynamically by Vasek

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.