10

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.

1 Answer 1

15

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.