No problem. Here ya go:

#!/usr/bin/perl use Tk; use Tk::Menu; # Create main window. $mw = MainWindow->new(); # Create menu. $menu = $mw->Menu(); # Add items to menu. $menu->add('command', -label => 'One', -command => \&item1); $menu->add('command', -label => 'Two', -command => \&item2); # Set up binding so that, when the right mouse button (3) # is clicked on the main window ($mw), &showmenu() is # called and is given the x and y coordinates of the click. $mw->bind('<3>', [\&showmenu, Ev('x'), Ev('y')]); # Start the program, with the main window in the front. $mw->focus(); MainLoop; # Called when right mouse button is clicked on the main window. sub showmenu { my ($self, $x, $y) = @_; $menu->post($x, $y); # Show the popup menu } # Called when menu items are selected. sub item1 { print "Item 1!\n" } sub item2 { print "Item 2!\n" }

Update: Added comments (sorry, I was late for work).

Update2: If, by saying "... so you can right click anywhere and see that menu popup ...", you mean that even clicking on widgets in the window, the pop-up would be displayed, I don't think that's very easy. I'm pretty sure that bindings are not inherited by child widgets, and the click is not passed-through to the parent, either. I'm not certain about this, and I'm at work now so I can't test it. If not, you could probably write some sort of loop that would cycle through all the children of a widget (aka window) and either create a binding that would pass-through the click event, or just copy the bindings from the parent to the child. Not sure how feasable this would be. Anyone have any more info on this, or whether bindings are inherited or events passed through?

Update3: To answer my own question... According to the docs for Tk::bind and Tk::bindtags, the default set of tags for a widget includes the nearest Toplevel ancestor. So, yes, bindings should be inherited (at least, that's the way I read it).

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) (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.