Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Break event chain in Tk::Canvas

by polettix (Vicar)
on May 27, 2006 at 09:57 UTC ( [id://551978]=perlquestion: print w/replies, xml ) Need Help??

polettix has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed Monks,

I'm trying to use Perl/Tk (in particular, Tk::Canvas) to make a little application for graph drawing. I need to put vertexes (represented by circles) and to connect them with edges (represented by lines). I want to do it with point-and-click.

I started with vertexes. When I click with the left button, I want the following behaviour:

  • if I click on an empty space, a new vertex should be created centered on the point;
  • if I click on an already-present vertex, it should be "selected".
I basically solved the first issue, as you may see in the following, but the second is still not satisfactory.

I tried to simplify things down to this (comment Smart::Comments out if you don't have it):

#!/usr/bin/perl use strict; use warnings; use Tk; use Smart::Comments; my $mw = MainWindow->new(); my $canvas = $mw->Canvas(-width => 300, -height => 300, -closeenough = +> 5); $mw->bind($canvas, '<1>', \&add_point); $canvas->bind('point', '<1>', \&toggle); $canvas->pack(); MainLoop(); sub add_point { ### adding a point... 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
When I click on an already-present point, I succeed in changing its color (i.e. "selecting" it) but I can't break the event chain. This results in a new vertex being always added, overlapping the toggled one.

I managed to solve the problem using a global status variable to track if I toggled or not, but this is really ugly and I hope there's a better way to solve this.

Thank you in advance for your patience,

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

Replies are listed 'Best First'.
Re: Break event chain in Tk::Canvas
by zentara (Archbishop) on May 27, 2006 at 11:20 UTC
    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
Re: Break event chain in Tk::Canvas
by BrowserUk (Patriarch) on May 27, 2006 at 10:30 UTC

    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.
Re: Break event chain in Tk::Canvas
by PodMaster (Abbot) on May 27, 2006 at 10:04 UTC
    '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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://551978]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-28 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found