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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.