Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tk menu checkbutton select()

Status
Not open for further replies.

xwax

Programmer
Oct 2, 2008
8
0
0
US
This exact question was asked in the following thread, but no answer was posted.

thread219-1283293

Can anyone please post the solution?
 
Code:
use Tk;

my $mw = MainWindow->new;

my $colorRed = 0;
my $colorGrn = 1;
my $colorBlu = 0;
my $colorYlw = 1;

my $menu = $mw->Menu (-type => 'menubar');
$mw->configure (-menu => $menu);

my $file = $menu->cascade (-label => '~Colors');

my $chk1 = $file->checkbutton (
   -label    => '~Red',
   -variable => \$colorRed,
   -onvalue  => 1,
   -offvalue => 0,
);

my $chk2 = $file->checkbutton (
   -label    => '~Green',
   -variable => \$colorGrn,
   -onvalue  => 1,
   -offvalue => 0,
);

my $chk3 = $file->checkbutton (
   -label    => '~Blue',
   -variable => \$colorBlu,
   -onvalue  => 1,
   -offvalue => 0,
);

my $chk4 = $file->checkbutton (
   -label    => '~Yellow',
   -variable => \$colorYlw,
   -onvalue  => 1,
   -offvalue => 0,
);

$file->separator;

my $exit = $file->command (
   -label => 'E~xit',
   -command => sub { exit; },
);

MainLoop;

Does this work?

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
ThanX

Yes, that works. But, I suppose the greater question is why can I do this ($mw -> as parent)...
Code:
use Tk;

my $mw = tkinit;

my $cb = $mw -> Checkbutton(-text => 'Word Wrap') -> pack;
$cb -> select;

MainLoop;
...but not this ($mbar -> as parent - GENERATES ERROR)...
Code:
use Tk;

my $mw = tkinit;

my $mbar = $mw -> Menu();
$mw -> configure(-menu => $mbar);

my $format = $mbar -> cascade(-label=>"Format", -underline=>0, -tearoff => 0);
my $cb = $format -> checkbutton(-text => 'Word Wrap');
$cb -> select;

MainLoop;

Maybe there is a way to reference $cb through the cget or entrycget methods. ??
 
Hm, actually I didn't know that select() was a method until just now.

I always selected/unselected a box by changing the variable.

Code:
$var = 1;
$cb = $mw->checkbutton (-variable => \$var, -onvalue => 1, -offvalue => 0);

# uncheck the box
$var = 0;

# check it again
$var = 1;

At any rate, it appears that Tk::Menu::checkbutton doesn't have these convenience methods built in, so you'll have to just switch the variable yourself.

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Boolean...
...just what I was looking for.

ThanX, Kirsle
 
It's not necessarily boolean, just the "onvalue" versus the "offvalue" i.e.

Code:
checkbutton (
   -onvalue => "yes",
   -offvalue => "no",
);

# if the var == yes, box is checked, otherwise unchecked

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Yes, you are right.

My usage just utilizes the default behavior of the on/off value, so this works fine for me. ThanX again.

Code Snippets:
Code:
our $wrap = 0;

...

our $format = $menu -> cascade(
    -label     => 'Format',
    -tearoff   => 0,
    -underline => 0,
    -menuitems => [
        [checkbutton => 'Word Wrap', -command  => \&format_wrap,
                                     -variable => \$wrap],
        [command     => 'Font',      -command  => \&format_font],
    ]
);

...

sub format_wrap {
    $txt -> configure(-wrap => $wrap ? 'word' : 'none');
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top