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

Hi Monks,
I am trying to delete a menu item from the menu, but unable to do that. In the below code, I want to delete "Name" Option when clicked on the Delete btn from the Main menu. Please let me know how to delete the menu items (Name, Website, Email) one by one when clicked on Delete option at run time.
use Tk; use strict; my $mw = new MainWindow; #Making a text area my $txt = $mw -> Scrolled('Text',-width => 30,-scrollbars=>'e') -> pac +k (); #Declare that there is a menu my $mbar = $mw -> Menu(); $mw -> configure(-menu => $mbar); my $others = $mbar -> cascade(-label =>"Main", -underline=>0, -tearoff + => 0); my $help = $mbar -> cascade(-label =>"Help", -underline=>0, -tearoff = +> 0); ## Others Menu ## my $insert = $others -> cascade(-label =>"Insert", -underline => 0, -t +earoff => 0); my $Btn1 = $insert -> command(-label =>"Name"); my $Btn2 = $insert -> command(-label =>"Website"); my $Btn3 = $insert -> command(-label =>"Email"); $others-> command(-label => "Delete", -underline=>0, -command=>sub {delete_menu();} ); MainLoop; sub delete_menu { $Btn1-> delete(0); }
But i am reveiving the below error message
Tk::Error: Can't locate object method "delete" via package "Tk::Menu:: +Button"

Replies are listed 'Best First'.
Re: Deleting Menu Item from Menu
by zentara (Cardinal) on Jan 28, 2009 at 14:38 UTC
    Update: It seems someone found the way above..way to go. I'll leave my post in here, in case you want to try and add stuff back in, as the $Btn1->parentMenu->delete(0); just lops off the first entry, and dosn't remove an entry by name( actually modifying the underlying AoA is still shrouded in mystery)

    See Tk menus

    I'm sorry I can't be of more help, but as the "hell" in the url suggests, Tk menus are a mess of deeply nested AoA's which are nearly impossible to access. See $menu->insert problem in Perl/Tk for an example of the complicated syntax needed to get into the nested data structure.

    I tried the $mbar->delete(2,1) syntax, and the best it does is destroy the cascade temporarily, but not permanently. Even if you could acess them, I don't know if you can delete menuitems once they are created. You may need to destroy the entire menu, and rebuild a new one. Usually, you just see items "greyed out", like

    You may find it easier just to switch menus, like


    I'm not really a human, but I play one on earth Remember How Lucky You Are
      .. just lops off the first entry, and dosn't remove an entry by name( actually modifying the underlying AoA is still shrouded in mystery)
      To reveal some, try $Btn1->[0]->delete(); errors
      Tk::Error: wrong # args: should be ".menu.main.insert delete first ?la +st?" at D:/Perl/site/lib/Tk.pm line 247. Tk callback for .menu.main.insert (menu invoke)
      Curious. To look up the name you need
      $Btn1->parentMenu->delete($Btn1->parentMenu->index($Btn1->[1])); # or $Btn1->[0]->delete($Btn1->[0]->index($Btn1->[1])); # or $$Btn1[0]->delete($$Btn1[0]->index($$Btn1[1])); # or $insert->menu->delete($insert->menu->index($$Btn1[1]));
      Yucky :)
        Here is a way to add to the menu
        sub add_menu{ # must be cascade, checkbutton, command, radiobutton, or separator $Btn1->parentMenu->add('command',-label => time, -command=>sub{print "3\n";}); }

        I'm not really a human, but I play one on earth Remember How Lucky You Are
        Thanks for that insight, every bit helps. It would be sweet to have a menuing system that used a tied AoH ( or similar) to build the menu, then have it automatically adjust to changes in the tied structure.

        I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Deleting Menu Item from Menu
by Anonymous Monk on Jan 28, 2009 at 13:50 UTC
    delete is a Tk::Menu method, not a Tk::Menu::Button
      Thanks for your quick response. But how to delete the menubutton from the menu?
        $Btn1->parentMenu->delete(0); # or equivalent $Btn1->[0]->delte(0);
        seems undocumented