Updated: Added code to example to include -command option for Checkbutton.

Updated Again: Created a second example based on my first, to try and more closely duplicate the scenario - I still don't see the same problem.

Why do you insist on making it difficult for people to help you? A small working example might have even shown you the solution before you posted the question. My guess is that you are setting the same variable for each Checkbutton because I don't see you incrementing t anywhere.

This example worked for me:

use strict; use Tk; my @cbValues; my $mw = MainWindow->new; $mw->Button( -text => "Print Selected", -command => \&printSelected )->pack; my $rows = $mw->Frame->pack; ## $rows will serve as a gridded container for each ## of the checkbutton rows. This should be roughly ## similar to using Tk::Table, but less hassle ## for a quick & dirty example. for my $row (0 .. 9) { $rows->Label(-text => $row)->grid( $rows->Checkbutton( -command => [\&select, $row], -variable => \$cbValues[$row])); } MainLoop; sub select { my $i = shift; if ($cbValues[$i]) { print "Selected $i\n"; } else { print "Deselected: $i\n"; } } sub printSelected { my $row = 0; print "\nSelected:\n"; foreach my $val (@cbValues) { print "$row selected\n" if $val; $row++; } }

Another example with a submit button for each row:

use Tk; use strict; my @cbValues; my $mw = MainWindow->new; $mw->Button( -text => "Print Selected", -command => \&printSelected )->pack; my $rows = $mw->Frame->pack; ## $rows will serve as a gridded container for each ## of the checkbutton rows. This should be roughly ## similar to using Tk::Table, but less hassle ## for a quick & dirty example. for my $row (0 .. 9) { $rows->Checkbutton( -text => $row, -variable => \$cbValues[$row] )->grid( $rows->Button( -text => "Submit", -command => sub { if ($cbValues[$row] == 1) { print "$row selected\n" } else { print "$row unselected\n"; } })); } MainLoop; sub printSelected { my $row = 0; print "\nSelected:\n"; foreach my $val (@cbValues) { print "$row selected\n" if $val; $row++; } }
Rob

In reply to Re: Array of TK::Checkbuttons not acting appropriately by rcseege
in thread Array of TK::Checkbuttons not acting appropriately by mikasue

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.