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

Dear monks

I am trying to modify my script below and add a Tk Checkbox to my pop-up menu. Unfortunately I am not able to do it. Somebody can help?

#use strict; use warnings; use Tk; my $mw = tkinit(); my $CkAutomaticBackUp; my $b1 = $mw->Button( -text => "Hello", ); $b1->grid(-row => 0, -column => 1,); Popup($mw, $b1); $mw->MainLoop; sub Popup{ my ($mw, $obj) = @_; my $menu = $mw->Menu(-tearoff=>0, -menuitems=>[ [command=>"Something", -command=>[sub {print "Hello"}, $obj,]], #[checkbutton=>(-label =>"Prompt for automatic backup",-variable=> +\$CkAutomaticBackUp,-onvalue=>'1', -offvalue=>'0', -compound => 'left +'), -command=>[sub {print "$CkAutomaticBackUp"}, $obj,]], ]); $obj->menu($menu); $obj->bind('<ButtonPress>', ['PostPopupMenu', Ev('X'), Ev('Y'), ]) +; return $obj; }

Replies are listed 'Best First'.
Re: Add Tk checkbox to popup menu
by choroba (Cardinal) on Jan 31, 2018 at 18:49 UTC
    You were pretty close. See the widget demo included in the Tk distribution for usage examples.
    my $menu = $mw->Menu( -tearoff => 0, -menuitems => [ [ command => "Something", -command => [ sub { print 'Hello' }, $ob +j ] ], [ checkbutton => "Prompt for automatic bac +kup", -variable => \$CkAutomaticBackUp, -onvalue => '1', -offvalue => '0', -compound => 'left', -command => [ sub { print "$CkAutomat +icBackUp" }, $obj ] ], ]);
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Perfect, thank you!