[Tkinter-discuss] Cursor coordinates

Michael O'Donnell michael.odonnell at uam.es
Fri Dec 4 19:31:07 CET 2009

Hi Vasilis,

A general method on all widgets;  w.winfo_pointerxy()

   Returns a tuple (x, y) containing the coordinates of the mouse
pointer relative  to w's root window.
   If the mouse pointer isn't on the same screen, returns (-1, -1).

Use this method on you canvas.
If you canvas is scrolled, then you need to adjust these coordinates
(since the top-left corner of the visible canvas may not be (0,0) of
the canvas coordinates)
So do something like:

        x= canv.canvasx(x)
        y= canv.canvasy(y)

Note also you can bind methods to events
(not only clicks on your canas, but also movement)
When the method is called, it gets an event object as one
parameter, and this event object includes the x and y of the cursor


I am workin from memory here, so don't tak my word for it.

On Fri, Dec 4, 2009 at 4:53 PM, Vasilis Vlachoudis
<Vasilis.Vlachoudis at cern.ch> wrote:
> Hi all,
> I have a canvas with some editable text (with create_text) in it.
> How can I get the cursor coordinates x,y from the canvas?
> The index() function returns only the character index, is there a way to
> convert the position of a character into x,y coordinates or bbox?
> Vasilis
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss

More information about the Tkinter-discuss mailing list