mpage has asked for the wisdom of the Perl Monks concerning the following question:
#!/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..
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: tk menu commands dont work while button pressed
by Sandy (Curate) on Jan 23, 2009 at 22:04 UTC | |
by mpage (Initiate) on Jan 23, 2009 at 22:14 UTC | |
|
Re: tk menu commands dont work while button pressed
by zentara (Cardinal) on Jan 23, 2009 at 20:56 UTC | |
by mpage (Initiate) on Jan 23, 2009 at 21:22 UTC |