Mouse Position Python Tkinter

You could set up a callback to react to <Motion> events:

import Tkinter as tk
root = tk.Tk()

def motion(event):
    x, y = event.x, event.y
    print('{}, {}'.format(x, y))

root.bind('<Motion>', motion)
root.mainloop()

I’m not sure what kind of variable you want. Above, I set local variables x and y to the mouse coordinates.

If you make motion a class method, then you could set instance attributes self.x and self.y to the mouse coordinates, which could then be accessible from other class methods.

Leave a Comment