Python, Tkinter: how to get coordinates on scrollable canvas

I have a Tkinter canvas with a scrollbar and some elements which, when I click on them, should return the coordinates. (Using Python.)

This works great with objects that are initially visible in the window. However, when I scroll down and the elements located lower on the canvas are visible, I do not get their canvas coordinates when I click, but in the coordinates of the window.

I cannot find information on how to get the absolute coordinates, so I wonder if anyone knows how to do this?

Thanks.

+6
source share
1 answer

Check out the documentation for the canvas widget here .

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

Here is an example callback function that converts the window coordinates x and y and prints the element closest to that position using 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) 
+6
source

Source: https://habr.com/ru/post/919555/


All Articles