7

I have a Tkinter canvas with a scrollbar, and some items that when I click them, it's supposed to return the coordinates. (Using Python.)

This works fine with the objects that's initially visible in the window. When I scroll down, however, and the items further down on the canvas come into view, I don't get their canvas coordinates when clicking, but the window coordinates.

I can't find info on how to get the absolute coordinates, so I'm wondering if anyone here knows how to do it?

Thanks.

Yngve
  • 743
  • 4
  • 10
  • 15

1 Answers1

12

Check out the documentation for the canvas widget here.

To convert from window coordinates to canvas coordinates, use the canvasx and canvasy methods.

Here is an example callback function which converts the window's x and y coordinates and prints the item closest to that position via the find_closest() method.

def callback(event):
    canvas = event.widget
    x = canvas.canvasx(event.x)
    y = canvas.canvasy(event.y)
    print canvas.find_closest(x, y)
gary
  • 4,227
  • 3
  • 31
  • 58
  • If the image is smaller than the canvas, then the above method gives me the absolute canvas co-ordinates, and not the co-ordinates of the image. Is there any way I can get the smaller image to fill the canvas or resize the canvas to display the image without any borders? – varagrawal May 30 '13 at 16:46