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

I use the following code to create a menubar ($menubar) with a cascade menu ($menu_links) and many 'command' items in it.
my $menubar = $main->Menu(-type => 'menubar'); $main->configure(-menu => $menubar); my $menu_links = $menubar->cascade(-label => "Links", -tearoff => 0); $menu_links->command(-label => 'test1', -command => \&sub1); $menu_links->command(-label => 'test2', -command => \&sub2); $menu_links->command(-label => 'test3', -command => \&sub3); ……
Then, I want to delete those items, eg. test1, test2, test3 ... but remain the cascade menu ($menu_links) to update the menu with new items.
I tried 'delete' and 'destroy' methods, but none of them work correctly.
Is there any way to do it? Or is it possible?

Replies are listed 'Best First'.
Re: How to remove items from a cascade menu in Tk?
by PodMaster (Abbot) on Jul 07, 2004 at 06:24 UTC
    Tk documentation is often incomplete. Always look at the source and at the examples and on perltk.org and usually you'll find an answer. This works
    use Tk; my $main = tkinit; my $menubar = $main->Menu(-type => 'menubar'); $main->configure(-menu => $menubar); my $menu_links = $menubar->cascade(-label => "Links", -tearoff => 0); $menu_links->command(-label => "Kill'em all", -command => sub{ $menu_links->menu->delete(2,5); }); $menu_links->separator(''); $menu_links->command(-label => 'test1', -command => \&sub1); $menu_links->command(-label => 'test2', -command => \&sub2); $menu_links->command(-label => 'test3', -command => \&sub3); MainLoop;

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Great! It works.
      It should be $menu_links->menu->delete(...);
      But I always try $menu_links->delete(...) directly, so it failed.
      Thanks a lot! ^_^
Re: How to remove items from a cascade menu in Tk?
by gri6507 (Deacon) on Jul 07, 2004 at 13:07 UTC
    I am sure you have good motivation to delete menu items at times, but here's a suggestion for concideration. Instead of deleting items, you could simply disable them using the entryconfigure() function and set state=>'disabled'. This may be less confusing for the users.