You're currently just pushing to an array when the checkbox is clicked, no matter whether the click checks or unchecks it. Instead of doing that, I'd suggest toggling a value in array, say $checkbox[$i] for the $i-th checkbox; set it to the checkbox's text when the box is selected, and undef otherwise.
Then when the "Connect" button is clicked and you want to ensure that exactly one checkbox has been selected, use grep to count how many elements of said array are defined.
Here's an example (I've also taken the liberty of formatting your script in a slightly more readable manner):
#!/usr/bin/perl use strict; use warnings; use feature qw/say/; #use diagnostics; use Tk; use Tk::Checkbox; my $mw = MainWindow->new; $mw->geometry("150x600"); $mw->title("ConMan"); # open my $FILE, '<', "ESSID", or die "Can't open file: $!"; # my @lines = <$FILE>; my @lines = <DATA>; my @checkbox; my $i = 0; foreach my $n (@lines) { chomp $n; my $number = $i++; # private copy for the anonymous sub below my $checkButton = $mw->Checkbutton( -text => "$n", -onvalue => 1, -offvalue => 0, -command => sub { $checkbox[$number] = defined $checkbox[$number] ? undef : +$n; }, )->pack( -side => 'top', -anchor => 'nw' ); } my $connButton = $mw->Button( -text => "Connect", -command => sub { my @networks = grep { defined } @checkbox; if(@networks == 1) { say $networks[0]; } else { say "Select one network, please."; } } )->pack( -side => 'left', -anchor => 'sw', -padx => '5', -pady => '5' ); my $CancelButtons = $mw->Button( -text => "Close", -command => sub { say "Closing out ConnMan."; $mw->withdraw; exit 0; } )->pack( -side => 'right', -anchor => 'se', -padx => '5', -pady => '5' ); MainLoop; __DATA__ line1 line2 line3 line4 line5
In reply to Re: reset checkbutton value
by AppleFritter
in thread reset checkbutton value
by amboxer21
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |