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:

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.

In reply to Break event chain in Tk::Canvas by polettix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.