Can anyone help with getting coordinates from a Tk.pm canvas? A mouse click in the lower window should put a mark on the canvas at that position. However, if the window has been scrolled away from top left, the marker is offset from the mouse position.

It appears that the coordinates obtained from the mouse click are relative to the viewable area only, but what I need are absoulte coordinates to the whole underlying canvas. I tried the methods that retreive scrollbar position, but they are not accurate enough to give me a good offset to the underlying canvas. The pods, Google and a couple of books have not provided any enlightenment. The following stripped down code illustrates the problem. Thanks.

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Frame; use Tk::Scrollbar; use Tk::Menu; use Tk::Menubutton; # marker position my $markX; my $markY; my $main = MainWindow->new; $main->geometry('800x600'); my $topPanel = $main->Frame(-width => 800, -height => 550)->pack; $topPanel->Button(-text => 'Quit', -width =>5, -command => [$main => ' +destroy'])->pack; # create scrolled canvas my $viewPanel = $main->Frame->pack; my $windowScrol = $viewPanel->Scrolled('Canvas', -width => 800, -height => 550, -scrollregion => [0, 0, 2000, 1000], -bg => 'white', -scrollbars => 'se')->pack; # get reference to actual underlying canvas my ($window, undef) = $windowScrol->Subwidget(); # binding to mouse click $window->Tk::bind("<1>" => [\&marker, Ev('x'), Ev('y')]); MainLoop(); sub marker{ my($id, $Mx, $My) = @_; my $color = '#800000'; my @items = $id->find('withtag', 'mark'); # if marker exists, move it to new position if ($items[0]){ my $Mx = $Mx - $markX; my $My = $My - $markY; for (@items){$id->move($_, $Mx, $My)} # or else create marker }else{ $id->create(('line', $Mx, $My-20, $Mx, $My+20), -fill => $color, -tags => ['mark'],); $id->create(('line', $Mx-20, $My, $Mx+20, $My), -fill => $color, -tags => ['mark'],); } $markX = $Mx; $markY = $My; }

In reply to Getting correct cursor position from scrolled Tk.pm canvas by hangon

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.