Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Selecting a single Checkbutton in Tk

by Anonymous Monk
on Jan 28, 2003 at 18:19 UTC ( [id://230675]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

Is it possible to select just one check button in Tk? I have two buttons, and I want to have the second one return to a normal state if the other is selected.

This is what I have for code

my $snmp_version1 = $right_frame->Checkbutton( -text => 'SNMPv1', -indicatoron => 'false', -selectcolor => 'green', -command => sub {$version = 1;}, ); $snmp_version1->pack(-padx => 5, -pady => 5,-anchor => 'w'); &BIND_MESSAGE($snmp_version1, "Send SNMP version 1 Traps"); my $snmp_version2 = $right_frame->Checkbutton( -text => 'SNMPv2', -indicatoron => 'false', -selectcolor => 'green', -command => sub {$version = 2; $snmp_version1->{'normal'}}, ); $snmp_version2->pack(-padx => 5, -pady => 5,-anchor => 'w'); &BIND_MESSAGE($snmp_version2, "Send SNMP version 2 Traps");
Any ideas why this won't work?

Thanks

Replies are listed 'Best First'.
(onvalue, offvalue, variable, or radiobutton) Re: Selecting a single Checkbutton in Tk
by bbfu (Curate) on Jan 28, 2003 at 20:33 UTC

    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

      Thanks bbfu!

      I was hoping it was as easy as that.
      Thanks again :)

Re: Selecting a single Checkbutton in Tk
by converter (Priest) on Jan 28, 2003 at 20:22 UTC

    Maybe you should look at Tk::Radiobutton, which will allow you to allocate a set of buttons so that only one button may be selected at a time. The example below is an abbreviation of the example included on page 84 of O'Reilly's Mastering Perl/Tk:

    $group1 = 100; for (qw/1 10 100 1000/) { $mw->Radiobutton( -text => $_, -variable => \$group1, -value => $_, )->pack( -side => 'left', ); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://230675]
Approved by defyance
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-26 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found