from tkinter import *
def customMouse(e=None):
x = e.x
y = e.y
label.place_configure(x=x,y=y-20)
root = Tk()
label = Label(master=root,text="^")
label.place(x=0,y=0)
root.bind('<Motion>',customMouse)
root.mainloop()
My problem is simple:
When I use e.x
or e.y
this returns the x
and y
coordinates of the mouse according to the widget over which the mouse is.
Also, I try using e.x_root
and e.y_root
this will return the mouse position of the desktop, not the Tkinter window.
My expected Output is that whenever my mouse moves then the label
will place on the mouse's position.
CodePudding user response:
If we subtract the coordinates of the upper left corner from the e.x ,y _root coordinates, then the label remains on the screen.
from tkinter import *
def customMouse(e=None):
x = e.x_root - root.winfo_x() - 10
y = e.y_root - root.winfo_y() - 40
print(x, y)
label.place_configure(x=x, y=y)
root = Tk()
label = Label(master=root, text="^", bg="grey")
label.place(x=0, y=0)
root.bind('<Motion>', customMouse)
root.mainloop()