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 |