I would just set an Enter-Leave flag on 'point'. The following global flag solves the problem efficiently. If you don't like the idea of a global flag, you can change your code to have just a single binding to <1>. Then in that callback, you can detect whether your pointer is over any items, see if the item is tagged 'point', and do what you need accordingly. Just do all your logic in 1 single callback, detection, creation, and toggling with ......if..elsif....elsif....elsif...else
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
my $inpoint = 0;
my $mw = MainWindow->new();
my $canvas = $mw->Canvas(-width => 300, -height => 300, -closeenough =
+> 5);
$mw->bind($canvas, '<1>', \&add_point);
$canvas->bind('point', '<Enter>', sub{ $inpoint = 1 });
$canvas->bind('point', '<Leave>', sub{ $inpoint = 0 });
$canvas->bind('point', '<1>', \&toggle);
$canvas->pack();
MainLoop();
sub add_point {
### adding a point...
return if $inpoint;
my ($canvas) = @_;
my $e = $canvas->XEvent;
my ($x, $y) = ($e->x, $e->y);
my $id = $canvas->createOval(
$x - 10, $y - 10, $x + 10, $y + 10,
-fill => 'blue',
-tags => ['point']
);
### new point created: "$id => [$x, $y]"
return;
} ## end sub add_point
sub toggle {
### toggle point...
my ($canvas) = @_;
my $item_id = $canvas->find('withtag', 'current')->[0];
### item id: $item_id
my $current_color = $canvas->itemcget($item_id, 'fill');
my $next_color = $current_color eq 'red' ? 'blue' : 'red';
$canvas->itemconfigure($item_id, -fill => $next_color);
Tk->break(); # Try to break, but with little luck...
return;
} ## end sub toggle
I'm not really a human, but I play one on earth.
flash japh
| [reply] [Watch: Dir/Any] [d/l] |
You'll need to look at using tk::bindtags to alter the order of evaluation for the bindings. I got it to work once, but it was mighty confusing. I'll look it out if you have as much trouble understanding the docs as I did?
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [Watch: Dir/Any] [d/l] |
'perldoc Tk::bind', read "MULTIPLE MATCHES", use Tk->break
update: I missed that you already have ->break.
Its better if you bind only 1 callback, and do your own checking to see if you've hit an existing point.
MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!" | I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README). | ** The third rule of perl club is a statement of fact: pod is sexy. |
| [reply] [Watch: Dir/Any] [d/l] |