Daria
Daria

Reputation: 3

Drawing on canvas with python and tkinter

I am trying to display mouse position on timer. I use winfo_pointerxy(), here is part of the code from my_func():

curr_x, curr_y = mouseFrame.winfo_pointerxy()
curr_x = mouseFrame.canvasx(curr_x)
curr_y = mouseFrame.canvasy(curr_y)
mouseFrame.create_oval(curr_x, curr_y, curr_x + 5, curr_y + 5, fill='green')
start_btn.after(time_interval, my_func)

It seems like I use canvasx() wrong cause it still returns position counted from the left-up corner of the screen.

Upvotes: 0

Views: 228

Answers (1)

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19144

According to this tkinter reference (which I use constantly)

Because the canvas may be larger than the window, and equipped with scrollbars to move the overall canvas around in the window, there are two coordinate systems for each canvas:

The window coordinates of a point are relative to the top left
corner of the area on the display where the canvas appears.

The canvas coordinates of a point are relative to the top left
corner of the total canvas.

If your canvas is against the upper left corner of the window (display) and you have not scrolled the canvas, the two sets of coordinates should be the same.

Upvotes: 1

Related Questions