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

The following pieces of code demonstrates my problem.

I have two cascades that contain radiobuttons under the Preference menubutton.

When I select a radiobutton, say option 1 under Cascade1, there will be a check mark beside option 1 all right. The problem is: if I continue to select another radiobutton that is located under Cascade2, say option A, I will have a new check mark beside A but the first check mark beside 1 under Cascade1 will be gone at the same time. How do I fix this problem?

Thanks

use strict; use warnings; use Tk; use subs qw/menubar_etal/; my $mw = MainWindow->new; $mw->configure(-menu => my $menubar = $mw->Menu(-menuitems => menubar_ +etal)); MainLoop; sub menubar_etal { [ map ['cascade', $_->[0], -menuitems => $_->[1]], ['~Preference', [ [qw/cascade Cascade1 -tearoff 0 -menuitems/ => [ map ['radiobutton', $_],qw/1 2 3 4/, + ], ], [qw/cascade Cascade2 -tearoff 0 -menuitems/ => [ map ['radiobutton', $_],qw/A B C D/, + ], ], ], ], ]; }
  • Comment on How do I select a Tk menu radiobutton without deselecting another?
  • Download Code

Replies are listed 'Best First'.
Re: How do I select a Tk menu radiobutton without deselecting another?
by keszler (Priest) on Oct 11, 2011 at 04:25 UTC

    From the docs:

    Whenever a particular entry becomes selected it stores a particular value into a particular global variable (as determined by the -value and -variable options for the entry).
    You need variables for each group:
    use strict; use warnings; use Tk; use subs qw/menubar_etal/; my ($g1,$g2); my $mw = MainWindow->new; $mw->configure(-menu => my $menubar = $mw->Menu(-menuitems => menubar_ +etal)); MainLoop; sub menubar_etal { [ map ['cascade', $_->[0], -menuitems => $_->[1]], ['~Preference', [ [qw/cascade Cascade1 -tearoff 0 -menuitems/ => [ map ['radiobutton', $_, -variable => \$g1],qw/1 2 3 4/, + ], ], [qw/cascade Cascade2 -tearoff 0 -menuitems/ => [ map ['radiobutton', $_, -variable => \$g2],qw/A B C D/, + ], ], ], ], ]; }

      Thanks keszler for the quick and great answer. Works like charm :) Thanks!