hello Chuma,
I'm not a master but, as you are starting with Tk maybe your are, if i'm permitted, misunderstanding something, or i misunderstand your question.
I mean that generally you do not care about where the cursor is, it's x and y , infact for your commodity you let the Tk geometry manager you have choosen to place your widget for you.
Infact when you create widget sensible to mouse like buttons you can specify directly an action to actuate when the button is clicked. You have not to care where the button is.
In the Tk world this is called callback and is a sub associated within the -command argument that many widgets support.
$mw->Button(-text => 'Quit', -command => sub { print 'Bye!'; exit;
+ })->pack;
By other hand keybinding are callbacks invoked to response to a keyboard event. In the following example you are saying that, for $mw , when the key a is released the Mainloop is charged to call a callback.
$mw->bind('<KeyRelease-A>' => \&print_something);
Obviously grabbing coordinates is possible in Tk and canvas are well suited to the task but is, in my opionion, an advanced usage not need in common Tk interfaces.
I have reduced this code to a simpler example to demonstrate how you can grab mouse coordinates when Button-1 of the mouse is pressed. It uses the advanced CanvasBind and grab Ev events printing coordinates to the terminal:
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
my $mw = MainWindow->new;
$mw->geometry("700x600");
my $canvas = $mw->Canvas->pack(-fill=>'both', -expand=>'both' );
$canvas->CanvasBind("<Button-1>", [ \&print_xy, Ev('x'), Ev('y') ]);
MainLoop;
sub print_xy {
my ($canv, $x, $y) = @_;
print "(x,y) = ", $canv->canvasx($x), ", ", $canv->canvasy($y), "\n"
+;
}
The original code is by zentara: you can SuperSearch all his code here around and learn a lot of things being a real master in Tk and many other fields.
In my learn path I found, recently, two modules very useful to understand many apsects of Tk
use Tk::WidgetDump;
use Tk::ObjScanner;
# and later
Tk::ObjScanner::scan_object($the_widget_or_window_to_inspect);
HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
|