in reply to $menu->insert problem in Perl/Tk

*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 ?

Replies are listed 'Best First'.
Re^2: $menu->insert problem in Perl/Tk
by thundergnat (Deacon) on Jun 10, 2005 at 10:43 UTC

    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 ?

        That sounds reasonable, but I can't figure out how to use entryconfigure to add child commands under the top level menu entry. If you can show me or point to a minmal working example it would be a huge help.

        Update: AHA! It turns out that entryconfigure IS usable to do what I am trying to do, it is just that the syntax of the command is quite arcane. I pieced this together from bits found in several places. Many thanks to spurperl for steering me in the right direction.

        #!/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) = @_; $menu->insert($index, 'cascade', -label => $name, -menu => $menu-> +entrycget($index, '-menu')->Menu( -tearoff => 0 )); my $entry = $menu->entrycget($index, '-menu'); map ( $entry->command ( -label => $_, -command => [sub {$widget->insert('insert', $_[0])}, $ +_], ) ,(qw/1 2 3 4 5/) ); }