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

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.