#!/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/)
);
}
####
#!/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/)
);
}
####
#!/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/)
);
}
####
Had to (re-)reate menu for bogus at F:/Perl/site/lib/Tk/Menu/Item.pm line 135.