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

Hello! Finally had a question I couldn't figure out or find on my own, so I request the wisdom of the keepers of Perl knowledge. I have a program which uses Tk::canvas, and I am trying to gather the coordinates of a mouse click on the canvas. What I would like to know is why the first bit of code works, where as numerous attempts at code similar to the second bit does not.

Works:
$canvas{'ref'}->CanvasBind("<1>", [ \&print_xy, Ev('x'), Ev('y') ]); sub print_xy{ my ($canv, $x, $y) = @_; print "(x,y) = ", $canv->canvasx($x), ", ", $canv->canvasy($y), "\n +"; }

Doesn't work:

$canvas{'ref'}->CanvasBind("<1>" => sub{ my $screenX = Ev('x'); my $screenY = Ev('y'); my $x = $canvas{'ref'}->canvasx($screenX); my $y = $canvas{'ref'}->canvasy($screenY); print "($x,$y)\n"; });
The second bit of code gives me 'bad screen distance x', which I know from trying is what I get when I dereference $x. I've tried various bits of dereferencing and such playfulness with the $screenX and $x values, and various things to try and get useful data, but it is all for not. I suspect there's something fundamental that is different which I don't know; can anyone shed some light on my conundrum? Thanks!

Replies are listed 'Best First'.
Re: Tk::canvas - Cavas coord problem
by ungalnanban (Pilgrim) on May 12, 2010 at 08:52 UTC
    Refer the following sample code I got it from net. I think this will help you.
    #!/usr/bin/perl -w use Tk; use strict; # Create B<MainWindow> and canvas my $mw = MainWindow->new; my $canvas = $mw->Canvas; $canvas->pack(-expand => 1, -fill => 'both'); # Create various items create_item($canvas, 1, 1, 'circle', 'blue', 'Jane'); create_item($canvas, 4, 4, 'circle', 'red', 'Peter'); create_item($canvas, 4, 1, 'square', 'blue', 'James'); create_item($canvas, 1, 4, 'square', 'red', 'Patricia'); # Single-clicking with left on a 'circle' item invokes a procedure $canvas->bind('circle', '<1>' => sub {handle_circle($canvas)}); # Double-clicking with left on a 'blue' item invokes a procedure $canvas->bind('blue', '<Double-1>' => sub {handle_blue($canvas)}); MainLoop; # Create an item; use parameters as tags (this is not a default!) sub create_item { my ($can, $x, $y, $form, $color, $name) = @_; my $x2 = $x + 1; my $y2 = $y + 1; my $kind; $kind = 'oval' if ($form eq 'circle'); $kind = 'rectangle' if ($form eq 'square'); $can->create( ($kind, "$x" . 'c', "$y" . 'c', "$x2" . 'c', "$y2" . 'c'), -tags => [$form, $color, $name], -fill => $color); } # This gets the real name (not current, blue/red, square/circle) # Note: you'll want to return a list in realistic situations... sub get_name { my ($can) = @_; my $item = $can->find('withtag', 'current'); my @taglist = $can->gettags($item); my $name; foreach (@taglist) { next if ($_ eq 'current'); next if ($_ eq 'red' or $_ eq 'blue'); next if ($_ eq 'square' or $_ eq 'circle'); $name = $_; last; } return $name; } sub handle_circle { my ($can) = @_; my $name = get_name($can); print "Action on circle $name...\n"; } sub handle_blue { my ($can) = @_; my $name = get_name($can); print "Action on blue item $name...\n"; }
Re: Tk::canvas - Cavas coord problem
by stefbv (Priest) on May 12, 2010 at 08:54 UTC
    Try something like this:
    use strict; use warnings; use Tk; my $main = new MainWindow; my $c = $main->Canvas( -width => 200, -height => 200, -relief => 'sunken', -borderwidth => 2, )->pack( -expand => 1, -fill => 'both', ); $c->CanvasBind( '<1>' => sub { my ($c) = @_; my $e = $c->XEvent; my $x = $c->canvasx( $e->x ); my $y = $c->canvasy( $e->y ); print "$x,$y\n"; } ); MainLoop;

    And it's better not to use Perl's reserved words for variable names or hash keys, in this case 'ref'.

      Thanks! The code does exactly what I'm looking for, guess I was just going about it slightly wrong. And yea, I suppose it's probably best not to use 'ref' to ensure it doesn't cause unexpected weirdness down the road, but it's just so darn convenient! ;)
Re: Tk::canvas - Cavas coord problem
by GenViper (Initiate) on May 12, 2010 at 07:45 UTC
    Well, apparently I was logged out when I made it, but I am the aforementioned 'Anonymous Monk'.