Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Tk and mouse position

by Popcorn Dave (Abbot)
on May 01, 2002 at 05:57 UTC ( [id://163212]=perlquestion: print w/replies, xml ) Need Help??

Popcorn Dave has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I am in need of a gentle nudge in the right direction here. Can anyone be so kind as to point me in the direction of some sample code or docs on how to find the cursor's position on a canvas, relative to the top left corner (0,0) in Tk?

I am currently playing with event and $widget->x and $widget->y but haven't got very far at all.

I have looked through the Tk docs and event was the closest thing I can find to use as a starting point.

I have also looked on CPAN but all I found was an interface for the Qt toolkit.

Anyone have any leads or done something like this? Is it even posible?

Replies are listed 'Best First'.
Re: Tk and mouse position
by ariels (Curate) on May 01, 2002 at 06:38 UTC

    Tk runs by MainLoop; it only talks to your app via events. The trick is to bind some event to a subroutine in your app, and then access the coordinates you want (probably via the x and y methods, or possibly rootx and rooty methods, of the Tk::event object your receive).

    A simple example

    #!/usr/local/bin/perl -w use strict; use Tk; my $mw = new Tk::MainWindow; my $c = $mw->Canvas->pack; my $tx = $c->createText(20, 10, -text => 'X: UNKNOWN'); my $ty = $c->createText(20, 25, -text => 'Y: UNKNOWN'); $c->Tk::bind('<Motion>' => [sub { my ($e,$x,$y) = (@_); $c->itemconfigure($tx, -text => "X: $x"); $c->itemconfigure($ty, -text => "Y: $y"); }, Ev('x'), Ev('y')]); MainLoop;
      Thanks for that!

      But I do have a couple of questions:

      1. I understand everything you've done except what $e does. Are there some docs that you can point me at to explain that particular point?
      2. What is the advantage or disadvantage in using pointerxy as opposed to what you wrote?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://163212]
Approved by virtualsue
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-19 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found