Vasek has asked for the wisdom of the Perl Monks concerning the following question:
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; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Strange behavior of iteration while creating Perl/Tk widgets dynamically
by kcott (Archbishop) on Oct 29, 2021 at 10:16 UTC | |
|
Re: Strange behavior of iteration while creating Perl/Tk widgets dynamically
by bliako (Abbot) on Oct 29, 2021 at 08:50 UTC | |
by BillKSmith (Monsignor) on Oct 29, 2021 at 14:34 UTC | |
by bliako (Abbot) on Oct 29, 2021 at 17:01 UTC | |
|
Re: Strange behavior of iteration while creating Perl/Tk widgets dynamically
by BillKSmith (Monsignor) on Nov 02, 2021 at 15:23 UTC |