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

Greetings fellow Monastarians...

I have been playing around with Perl/Tk widgets in general, menus in specific, and have run into an issue that I just can't seem to overcome.

Say you have a widget that has a menu, a text widget, for instance, and you want to add a custom option menu to the existing menu. That's pretty simple. Here's a minimal example.

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Text; my $top = MainWindow->new; my $text = $top->Text->pack(-expand => 'yes', -fill => 'both'); my $menu = $text->menu; add_my_menu_item($menu, 'bogus', $text); $top->configure(-menu => $menu); MainLoop; sub add_my_menu_item{ my ($menu, $name, $widget) = @_; my $entry = $menu->Cascade( -label => $name); map ( $entry->command ( -label => $_, -command => [sub {$widget->insert('insert', $_[0])}, $ +_], ) (qw/1 2 3 4 5/) ); }

No problem. Works great. But.... Say you wanted to insert the new custom menu option at someplace other than the end of the menu. How to do that? Well, Tk::Menu provides an insert method, but when you insert a menu item with the insert method, it doesn't return a reference. And I can't figure out any (good) way to get a reference to it once it has been created.

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Text; my $top = MainWindow->new; my $text = $top->Text->pack(-expand => 'yes', -fill => 'both'); my $menu = $text->menu; add_my_menu_item($menu, 2, 'bogus', $text); $top->configure(-menu => $menu); MainLoop; sub add_my_menu_item{ my ($menu, $index, $name, $widget) = @_; my $entry = $menu->insert($index, 'cascade', -label => $name); return unless defined $entry; # No returned ref! map ( $entry->command ( -label => $_, -command => [sub {$widget->insert('insert', $_[0])}, $ +_], ) ,(qw/1 2 3 4 5/) ); }

Now I have found a (broken, ugly) work-around. It works but throws a warning that can't be suppressed without redirecting STDERR. I'm creating the menu item at the end of the menu, deleting it, which undefs the hash table for that entry, then inserting the item with the same name at the index I want it. The names collide in the hash, the new menu item gets written into the location formerly held by the old entry but I now have a reference which points to the memory location now occupied by the inserted item so I can populate the cascade menu.

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Text; my $top = MainWindow->new; my $text = $top->Text->pack(-expand => 'yes', -fill => 'both'); my $menu = $text->menu; add_my_menu_item($menu, 2, 'bogus', $text); $top->configure(-menu => $menu); MainLoop; sub add_my_menu_item{ my ($menu, $index, $name, $widget) = @_; my $entry = $menu->Cascade( -label => $name); $menu->delete('last'); $menu->insert($index, 'cascade', -label => $name); map ( $entry->command ( -label => $_, -command => [sub {$widget->insert('insert', $_[0])}, $ +_], ) ,(qw/1 2 3 4 5/) ); }

Does anyone know of a better way to do this? I've been trying to figure it out for about 3 days now and am not much further than when I started.

For those interested: The warning I am getting is

Had to (re-)reate menu for bogus at F:/Perl/site/lib/Tk/Menu/Item.pm l +ine 135.

The misspelling in the warning and the fact that it can't be suppressed leads me to believe that it might just be a left-over debugging warning. I'm running Active Perl 5.8.6 with Tk804.027 under Win2k & WinXP

Thanks

Replies are listed 'Best First'.
Re: $menu->insert problem in Perl/Tk
by spurperl (Priest) on Jun 10, 2005 at 05:21 UTC
    *Why* do you need that reference, though ? You can do anything you want with the menu by using the $index for it. Can't you construct the commands before you insert it, and add them later ?

      Ah. But how to do that? I can find no examples of how to do that in the perl docs, in Mastering Perl/Tk, or by STFW. There is plenty on how to find the index of a menu item, but little on how to manipulate the menu item once you have the index of it.

        entryconfigure ?