You need to use the -onvalue, -offvalue, and -variable options for the checkbutton. The code below has an example. Alternatively, you could manually call the deselect() method of the other checkbuttons in the command for each checkbutton but that's a lot more work on your part.

Logically, though, if you're only going to have one selected at a time, it makes more sense to use a Tk::RadioButton, as that is the whole idea behind RadioButtons versus Checkbuttons. The code has an example of using RadioButtons as well.

#!/win2k/Perl/bin/perl use warnings; use strict; use Tk; use Tk::Checkbutton; use Tk::Radiobutton; our $var1 = 1; our $var2 = 1; my $mw = Tk::MainWindow->new(); my ($check1, $check2); $check1 = $mw->Checkbutton( -text => 'Check 1', -onvalue => 1, # -offvalue => 2, # Update: Don't need this -variable => \$var1, )->pack(); $check2 = $mw->Checkbutton( -text => 'Check 2', -onvalue => 2, # -offvalue => 1, # Update: Don't need this -variable => \$var1, )->pack(); # Or use a radio button... my ($radio1, $radio2); $radio1 = $mw->Radiobutton( -text => 'Radio 1', -value => 1, -variable => \$var2, )->pack(); $radio2 = $mw->Radiobutton( -text => 'Radio 2', -value => 2, -variable => \$var2, )->pack(); MainLoop; print "Var1: $var1\nVar2: $var2\n";

Update: On second thought, you don't actually need to use -offvalue at all. Just set an -onvalue for each checkbox and bind them all to the same variable and it works fine.

bbfu
Black flowers blossum
Fearless on my breath


In reply to (onvalue, offvalue, variable, or radiobutton) Re: Selecting a single Checkbutton in Tk by bbfu
in thread Selecting a single Checkbutton in Tk by Anonymous Monk

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.