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

I use Tk::MenuButton and I have this code:
my $TOP=Tk::MainWindow->new; my $tp=$TOP->Toplevel; $tmb=$tp->MenuButton(-text=>'',-tearoff=>0)->pack; foreach my $d (@dir) { $tmb->command(-label=>decode('utf8',$d),-command=>sub { ... }) };
In some place of code I need delete all menu items(created by method 'command') and create new menu items.
For this I use command:
$tmb->configure(-menu=>'');
After this code I have deleted all menu items. But -tearoff=>0 now not working(I see line in menubutton menu) and if I use this parameter then I have error like "this is wrong parameter".
How I can properly clear my menu items?

Replies are listed 'Best First'.
Re: delete menu items im Tk::MenuButton
by zentara (Cardinal) on Dec 05, 2010 at 20:01 UTC
    There probably is a way to dig into the Menubutton structure to remove the added menubuttons, but it probably would be easier to somehow packForget the menu, then rebuild it. Otherwise, it's probably better to use something that allows the -options to be an array reference. So all you need do to change it, it to change arrays.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); my @opt1 = ('a'..'m'); my $var = 'a'; my $tvar = 'a'; my $opt = $mw->Optionmenu( -command => \&show_choice, -variable => \$var, -textvariable => \$tvar, -options => \@opt1, )->pack; $mw->Button(-text=>'Change Options',-command=>[\&change_ops, $opt])->p +ack; $mw->Button(-text=>'Exit', -command=>sub{$mw->destroy})->pack; MainLoop; sub show_choice { print "got: ", shift, "\n" } sub change_ops { my $op = shift; my @newoptions =(); $tvar = 'null'; $op->configure( -options => \@newoptions ); }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks, but Optionmenu method not interesting for me.
      I use in my program corrected Tk::MenuButton(corrected cursor style and relief style. by default this two parameters can't be changed in Options pooldown menu too) for menus.
      In Option menu this problem persent too...
        I'm find brutal method to redraw menubutton menu.
        I'm destroy menubutton and create it again and set it needed place with method pack(-before...).