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.

In reply to Re: Tk - finding what's being pointed at by Discipulus
in thread Tk - finding what's being pointed at by Chuma

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.