Use the canvasx
and canvasy
, giving a zero argument to calculate x / y of the upper left corner of the visible canvas. Then just use the math to transform the coordinates of the canvas into something relative to the window.
For example, if the canvas scrolls to the very top and to the left, x0 and y0 will be zero. Any coordinate of the canvas will be the same as the coordinate of the window (i.e., the canvas x / y 0,0 will correspond to the coordinate of the window 0,0).
If you scrolled 100 pixels down and to the right, the canvas coordinate 100 100 will be transferred to the window coordinate 0,0, since this is the pixel that is in the upper left corner of the window.
This gives you a value relative to the upper left corner of the canvas. If you need this relative to the upper left corner of the whole window, use winfo_x
and winfo_y
to get the canvas coordinate relative to the window and do a little more math. Or use winfo_rootx
and winfo_rooty
to get the widget coordinates relative to the screen.
source share