hangon has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting correct cursor position from scrolled Tk.pm canvas
by hangon (Deacon) on Jul 04, 2007 at 09:38 UTC |