Hello Monks,

I'm having problems with a menu checkbutton. It seems that in a particular case, a checkbutton is not changing the value of the associated variable.

I am coding with strawberry perl 5.30 on windows 10.

In the code below, the thing1 checkbutton is associated with the variable $self->{SETTINGS}->{thing1}. The thing2 checkbutton is associated with the variable $self->{thing2}.

If I click on the thing2 checkbutton, the value of $self->{thing2} is changed, as I would expect. But if I click on the thing1 checkbutton, the value of $self->{SETTINGS}->{thing1} is NOT changed.

Can anyone explain with this is the case? Your help would be appreciated.

use warnings; use strict; { package OptionsTest; use Cwd; use Tk qw(MainLoop); require Tk::ROText; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = bless { @_ }, $class; my $mw = MainWindow->new(); $self->{MW} = $mw; $self->MakeWidgets; $mw->configure(-title => "Options Test"); my $menu = $mw->Menu(-type => 'menubar'); $mw->configure(-menu => $menu); $self->BuildMenu($menu); $self->{SETTINGS} = { thing1 => 7 }; $self->{thing2} = 3; MainLoop(); } sub BuildMenu { my ($self, $menu) = @_; $menu->Cascade( -label => 'Options', -tearoff => 0, -menuitems => [ ['checkbutton', 'thing1', -variable => \$self->{SETTINGS}->{thing1} +, -command => sub { $self->AdjustConsole(); }], ['checkbutton', 'thing2', -variable => \$self->{thing2}, -command = +> sub { $self->AdjustConsole(); }], ] ); } sub MakeWidgets { my ($self) = @_; my $frm = $self->{MW}->Frame()->pack(qw(-side top -fill both -expand + 0)); $self->{CONSOLE} = $frm->Scrolled('ROText', -font => "{Courier New} 12 bold", -background => 'DarkBlue', -foreground => 'OldLace', -scrollbars => 'se', -wrap => 'none', -height => 10, -width => 60, -tabs => ['2'], )->pack(-expand => 1, -fill => 'both', -anchor => ' +n' ); tie *STDOUT, 'Tk::Text', $self->{CONSOLE}; # Send STDOUT to console tie *STDERR, 'Tk::Text', $self->{CONSOLE}; # Send STDERR to console + } sub AdjustConsole { my ($self) = @_; printf( "thing1: %s %s thing2: %s %s\n", \$self->{SETTINGS}->{thing1}, $self->{SETTINGS}->{thing1}, \$self->{thing2}, $self->{thing2}); } } OptionsTest->new();

In reply to menu checkbutton is not modifying variable by CrashBlossom

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.