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.

Answer from unutbu on Stack Overflow
Discussions

python - How to show live mouse position on tkinter window - Stack Overflow
But I need the label to keep updating as and when the mouse moves. Help will be appreciated Thank you! ... You don't need win32api to get the cursor coordinates. Tkinter has methods to do that in a cross-platform way. ... No need to use win32api, tkinter has it built in. We can bind a function to root's key and use the given positional ... More on stackoverflow.com
🌐 stackoverflow.com
python - Tkinter get mouse coordinates on click and use them as variables - Stack Overflow
I am completely new to Python. Therefore don't get too mad at me, because I am sure that there are basic things that I am missing. Here is my problem: I am trying to extract mouse-click coordinate... More on stackoverflow.com
🌐 stackoverflow.com
python - How do I get mouse position relative to the parent widget in tkinter? - Stack Overflow
I need to get the mouse position relative to the tkinter window. More on stackoverflow.com
🌐 stackoverflow.com
How do I determine if the mouse is over canvas objects (images, rectangles, etc.)?
Usually you let the object report that the mouse is above it by binding to "enter", instead of polling all your objects. What are you trying to do? More on reddit.com
🌐 r/Tkinter
6
2
October 21, 2021
🌐
Reddit
reddit.com › r/learnpython › questions about getting mouse coordinates in tkinter
r/learnpython on Reddit: Questions about getting mouse coordinates in Tkinter
April 25, 2011 -

Hey guys

Uni assignment here. I have to write a class that gets the cursor position and I have absolutely no idea where to start. I'm tearing my hair out because every god damn google link is purple and I've gotten nowhere.

I only know how to input things through the keyboard. How on Earth can I make it take information from the mouse? Everything I've ever done has had some sort of input either through IDLE or in the .py file.

Cheers

🌐
GitHub
gist.github.com › 3810848
Tkinter mouse example · GitHub
can you tell how to get x and y coordinates when i am not moving the mouse pointer and when i have not pressed any button? ... I haven't tried but https://stackoverflow.com/questions/22925599/mouse-position-python-tkinter may give you an idea
🌐
TutorialsPoint
tutorialspoint.com › article › mouse-position-in-python-tkinter
Mouse Position in Python Tkinter
1 month ago - To print the coordinates of the pointer, we bind the <Motion> event with a callback function that gets the position in x and y variables. Here's how to track mouse position and display coordinates in the console ?
🌐
Medium
medium.com › python-other › used-in-combination-withtkinter-and-pyautogui-to-get-the-cursor-position-in-real-time-af71a36d556c
Used in combination with’tkinter ‘and’pyautogui’ to get the cursor position in real time | by PointCloud-Slam-Image-Web3 | Python & Other | Medium
April 19, 2024 - To get the cursor position in real time using the combination of tkinter and pyautogui, you can use the’after () ‘method of’tkinter’ and the’position () ‘method of’pyautogui’.
Find elsewhere
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 194446 › tkinter-set-mouse-position
python - Tkinter set mouse position? [SOLVED] | DaniWeb
We are talking about the Tkinter GUI toolkit. There are no such methods. ... from ctypes import * user = windll.user32 x = 640 y= 480 user.SetCursorPos(x,y) — Tech B 48 Jump to Post ... You maybe want "focus" or "geometry" depending on what you mean by "mouse position".
Top answer
1 of 2
9

If what I said in my previous comment is what you're trying to do, since tkinter doesn't pause the program to wait for a mouse click event you will have to do this: it rebinds it every time the mouse button get clicked

from tkinter import *
from tkinter.filedialog import askopenfilename
from PIL import Image, ImageTk
import tkinter.simpledialog

root = Tk()

#setting up a tkinter canvas
w = Canvas(root, width=1000, height=1000)
w.pack()

#adding the image
File = askopenfilename(parent=root, initialdir="./",title='Select an image')
original = Image.open(File)
original = original.resize((1000,1000)) #resize image
img = ImageTk.PhotoImage(original)
w.create_image(0, 0, image=img, anchor="nw")

#ask for pressure and temperature extent
xmt = tkinter.simpledialog.askfloat("Temperature", "degrees in x-axis")
ymp = tkinter.simpledialog.askfloat("Pressure", "bars in y-axis")

#ask for real PT values at origin
xc = tkinter.simpledialog.askfloat("Temperature", "Temperature at origin")
yc = tkinter.simpledialog.askfloat("Pressure", "Pressure at origin")

#instruction on 3 point selection to define grid
tkinter.messagebox.showinfo("Instructions", "Click: \n" 
                                            "1) Origin \n"
                                            "2) Temperature end \n"
                                            "3) Pressure end")

# From here on I have no idea how to get it to work...

# Determine the origin by clicking
def getorigin(eventorigin):
    global x0,y0
    x0 = eventorigin.x
    y0 = eventorigin.y
    print(x0,y0)
    w.bind("<Button 1>",getextentx)
#mouseclick event
w.bind("<Button 1>",getorigin)

# Determine the extent of the figure in the x direction (Temperature)
def getextentx(eventextentx):
    global xe
    xe = eventextentx.x
    print(xe)
    w.bind("<Button 1>",getextenty)

# Determine the extent of the figure in the y direction (Pressure)
def getextenty(eventextenty):
    global ye
    ye = eventextenty.y
    print(ye)
    tkinter.messagebox.showinfo("Grid", "Grid is set. You can start picking coordinates.")
    w.bind("<Button 1>",printcoords)

#Coordinate transformation into Pressure-Temperature space
def printcoords(event):
    xmpx = xe-x0
    xm = xmt/xmpx
    ympx = ye-y0
    ym = -ymp/ympx

    #coordinate transformation
    newx = (event.x-x0)*(xm)+xc
    newy = (event.y-y0)*(ym)+yc

    #outputting x and y coords to console
    print (newx,newy)

root.mainloop()
2 of 2
3

The easiest way is to set x, y to global, class or not doesn't matter. I didn't see your full code because I can't open zip files on my phone. So here's what I can help with your example

import tkinter as tk
def getorigin(eventorigin):
      global x,y
      x = eventorigin.x
      y = eventorigin.y
      print(x,y)

root = tk.Tk()
root.bind("<Button 1>",getorigin)
🌐
CodePal
codepal.ai › code-generator › query › lBWFtB8t › python-tkinter-mouse-position
Python Tkinter Mouse Position - CodePal
import tkinter as tk def get_mouse_position(): """ Function to get the position of the mouse when the left mouse button is clicked. Returns: - tuple: A tuple containing the x and y coordinates of the mouse position.
🌐
TutorialsPoint
tutorialspoint.com › article › getting-the-cursor-position-in-tkinter-entry-widget
Getting the Cursor position in Tkinter Entry widget
1 month ago - # Import required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter window win = Tk() win.geometry("700x350") win.title("Get the Cursor Position") # Create an instance of style class style = ttk.Style(win) # Function to retrieve the current position of the cursor def get_current_info(): cursor_pos = entry.index(INSERT) print("The cursor is at:", cursor_pos) result_label.config(text=f"Cursor position: {cursor_pos}") # Create an entry widget entry = ttk.Entry(win, width=18) entry.pack(pady=30) # Create a button widget button = ttk.Button(win, text="Get Info", command=get_current_info) button.pack(pady=10) # Label to display cursor position result_label = ttk.Label(win, text="Click 'Get Info' to see cursor position") result_label.pack(pady=10) win.mainloop()
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › community › general discussion
Detect mouse coordinates OUTSIDE of any GUI using Python - Raspberry Pi Forums
#!/usr/bin/python import struct import binhex # You'll need to find the name of your particular mouse to put in here...
🌐
CopyProgramming
copyprogramming.com › howto › python-python-tkinter-get-mouse-position-code-example
Python Tkinter Get Mouse Position: Complete Code Guide & 2026 Best Practices
December 3, 2025 - Event binding (<Motion>, <B1-Motion>) is the most efficient method for tracking mouse position in tkinter applications
🌐
Python
mail.python.org › pipermail › python-dev › 2003-April › 035149.html
[Python-Dev] Getting mouse position interms of canvas unit.
September 11, 2013 - How can i get mouse position values interms of canvas unit? My program is given below. ***************************** from Tkinter import * root = Tk() c = Canvas(root,width="300m",height="300m",background = 'gray') c.pack() def mouseMove(event): print c.canvasx(event.x), c.canvasy(event.y) c.create_rectangle('16m','10.5m','21m','15.5m',fill='blue') c.bind('<Motion>',mouseMove) root.mainloop() Tnanx __________________________________ Do you Yahoo!?
🌐
Tkinterexamples
tkinterexamples.com › events › mouse
Mouse Events - Tkinter Examples
We can bind to mouse movement by using widget.bind("<Motion>", motion_handler).This is a very noisy (registers on every pixel moved) and imprecise (but not quite every pixel) event so we cannot recommend it for general use. This will register the correct position ...
🌐
Python Forum
python-forum.io › thread-40319.html
Howto do motion event on solely window and not the widgets on it?
I have a single window (root): window = customtkinter.CTk() which holds several widges as (mapview,labels,combos....). I use this function to get mouse x and y position, while mouse is moving: It show
🌐
GeeksforGeeks
geeksforgeeks.org › change-the-position-of-cursor-in-tkinters-entry-widget
Change the position of cursor in Tkinter’s Entry widget | GeeksforGeeks
April 5, 2021 - Is a lot of text causing an issue to you to move at any other position in it? Don't worry, we have a solution to thi ... Tkinter is a GUI (Graphical User Interface) module which is widely used to create GUI applications. It comes along with the Python itself. Entry widgets are used to get the entry from the user.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to track mouse position in python tkinter
5 Best Ways to Track Mouse Position in Python Tkinter - Be on the Right Side of Change
March 6, 2024 - The winfo_pointerxy() function of a Tkinter window retrieves the current mouse position as a tuple of the form (x, y). This doesn’t require an event binding and can be used at any point in your program to get the cursor’s position relative ...