I'd say, especially if your menu will contain mostly the same stuff except for the label text, you should definately re-use the popup menu. Update: Though, if your menus are going to be completely different depending on where the user clicked, it would probably make more sense to pre-generate all the menus. Unless you have a lot of them. I guess, uh, it just depends. ;-)
As for how to tell what label was under the mouse, just use the 'W' parameter to Ev(), like so:
# this is all documented under Tk::bind $mw->bind('<3>', [\&callback, Ev('X'), Ev('Y'), Ev('W')]);
...and it should pass you a reference to the widget that was clicked upon (as the fourth parameter).
Update: Oh, and, uh, you'd have to alter the menu within the callback that shows it (just before posting) because I don't know of an easy way to pass an argument (ie, the clicked widget) to the postcommand() method.
Update2: Uh, like this: :-)
#!/usr/bin/perl use Tk; use Tk::Menu; $mw = MainWindow->new(); $menu = $mw->Menu(-tearoff => 0); $menu->add('separator'); $menu->add('command', -label => 'One', -command => \&item1); $menu->add('command', -label => 'Two', -command => \&item2); $mw->Label(-text => 'Label 1')->pack(); $mw->Label(-text => 'Label 2')->pack(); $mw->Label(-text => 'Label 3')->pack(); $mw->bind('<3>', [\&showmenu, Ev('X'), Ev('Y'), Ev('W')]); $mw->focus(); MainLoop; sub showmenu { my ($self, $x, $y, $widget) = @_; my $label = $widget->cget('text'); $menu->insert(0, 'command', -label => $label, -command => sub { print "Clicked $label.\n" }, ); $menu->post($x, $y); $menu->delete(0,0); } sub item1 { print "Item 1!\n" } sub item2 { print "Item 2!\n" }
bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.
In reply to (bbfu) (reusing menus) Re2: (bbfu) (here's an example) Re: perl tk popup menu
by bbfu
in thread perl tk popup menu
by Eradicatore
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |