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:
I tried to simplify things down to this (comment Smart::Comments out if you don't have it):
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.#!/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
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
In reply to Break event chain in Tk::Canvas by polettix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |