[Tkinter-discuss] Cursor coordinates

Vasilis Vlachoudis Vasilis.Vlachoudis at cern.ch
Fri Dec 4 20:49:01 CET 2009

Thank you Michael, I was looking also for that function but for another task.

On my previous email I was asking to find the position (x,y) of the text-cursor or alternative the bbox of a single character from a text element inside a canvas. In my project I have created a full text editor inside the canvas for floating text elements.

# create a text element
txt = canvas.create_text(xt,yt,text="blahblah)

# place the cursor on the fifth character
canvas.icursor(txt, 5)

# get the position
print "cursor character position=",canvas.index(txt, INDEX)

But how do I get the canvas x,y of the cursor?

Currently I am making a binary search within the limits of the bounding box searching for the x,y corresponding to the character location returned by the index() method.

I was asking if there is an existing function like the index() but to return the x,y position in the canvas.


-----Original Message-----
From: micko.madrid at gmail.com on behalf of Michael O'Donnell
Sent: Fri 4/12/2009 19:31
To: Vasilis Vlachoudis
Cc: tkinter-discuss at python.org
Subject: Re: [Tkinter-discuss] Cursor coordinates
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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20091204/19bdb3a7/attachment.htm>

More information about the Tkinter-discuss mailing list