in reply to perl tk popup menu
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: (bbfu) (here's an example) Re: perl tk popup menu
by Eradicatore (Monk) on Nov 09, 2001 at 03:35 UTC | |
by bbfu (Curate) on Nov 09, 2001 at 04:06 UTC | |
by Anonymous Monk on Feb 12, 2008 at 18:00 UTC |