Hi,
I have an application which will make a menu popup when you click button 1 which contains commands to do stuff...but I want it to disappear when button 1 is released. The effect I want is that while holding down the B1 you can drag your mouse down the list of commands and let go over one of them to initiate it and if you let go anywhere else (i.e. not over a menu command) the menu disappears. I bind B1-ButtonRelease to do an unpost() on the menu which is fine but while holding the button down the commands under the mouse wont activate...any ideas? Here's my code:
#!/usr/bin/env perl
use Tk;
our $mw = MainWindow->new();
$mw->geometry('750x500');
$mw->Label(-text => 'Label 1')->pack();
$mw->Label(-text => 'Label 2')->pack();
$mw->Label(-text => 'Label 3')->pack();
$menu = $mw->Menu(-tearoff => 0);
$menu->add('separator');
$menu->add('command', -label => 'One', -command => \&item1);
$menu->add('command', -label => 'Two', -command => \&item2);
$mw->bind('<3>', \&WorkspaceRightClick, Ev('X'), Ev('Y'), Ev('W'));
$mw->bind('<B3-ButtonRelease>', \&WorkspaceRightClickRelease, Ev('X'), Ev('Y'), Ev('W'));
sub WorkspaceRightClick {
my ($self, $x, $y, $widget) = @_;
my $label = $widget->cget('-text');
print "LABEL OF WIDGET UNDER ME IS $label\n";
$menu->insert(1, 'command',
-label => $label,
-command => sub { print "Clicked $label.\n" },
);
$menu->post($x, $y);
$menu->delete(0,0);
}
sub WorkspaceRightClickRelease {
my ($self, $x, $y, $widget) = @_;
$menu->unpost();
}
sub item1 { print "Item 1!\n" }
sub item2 { print "Item 2!\n" }
MainLoop;
NOTE : if you comment out the ButtonRelease binding you can see the commands work .. but not while holding down the mouse..
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.