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:
Robuse 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++; } }
In reply to Re: Array of TK::Checkbuttons not acting appropriately
by rcseege
in thread Array of TK::Checkbuttons not acting appropriately
by mikasue
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |