Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Mouse Position in Python Tkinter
Events are very useful to perform and manage multiple tasks in large-scale applications. We can bind a particular event with keyboard buttons or mouse buttons using the bind('handler', 'callback') method. Mouse pointer motion tracking is commonly used for building screensavers, 2D or 3D games, and interactive applications. 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.
Basic Mouse Position Tracking
Here's how to track mouse position and display coordinates in the console ?
# Import tkinter library
from tkinter import *
# Create an instance of tkinter frame or window
win = Tk()
# Set the geometry of tkinter frame
win.geometry("750x250")
win.title("Mouse Position Tracker")
def callback(e):
x = e.x
y = e.y
print("Pointer is currently at %d, %d" % (x, y))
# Bind the Motion event to the callback function
win.bind('<Motion>', callback)
win.mainloop()
The output will be printed to the console as you move your mouse over the window ?
Pointer is currently at 452, 225 Pointer is currently at 426, 200 Pointer is currently at 409, 187 Pointer is currently at 392, 174 Pointer is currently at 382, 168 Pointer is currently at 378, 163
Displaying Coordinates on the Window
Instead of printing to console, you can display coordinates directly on the window using a Label ?
from tkinter import *
win = Tk()
win.geometry("750x250")
win.title("Mouse Position Display")
# Create a label to display coordinates
coord_label = Label(win, text="Move mouse to see coordinates", font=("Arial", 14))
coord_label.pack(pady=20)
def show_coordinates(event):
x, y = event.x, event.y
coord_label.config(text=f"Mouse Position: X={x}, Y={y}")
# Bind mouse motion to the function
win.bind('<Motion>', show_coordinates)
win.mainloop()
Mouse Click Events
You can also track mouse click events along with position ?
from tkinter import *
win = Tk()
win.geometry("750x250")
win.title("Mouse Click Tracker")
status_label = Label(win, text="Click anywhere on the window", font=("Arial", 12))
status_label.pack(pady=20)
def on_left_click(event):
status_label.config(text=f"Left clicked at: X={event.x}, Y={event.y}")
def on_right_click(event):
status_label.config(text=f"Right clicked at: X={event.x}, Y={event.y}")
# Bind different mouse events
win.bind('<Button-1>', on_left_click) # Left click
win.bind('<Button-3>', on_right_click) # Right click
win.mainloop()
Common Mouse Events
| Event | Description | Usage |
|---|---|---|
<Motion> |
Mouse movement | Track pointer position |
<Button-1> |
Left mouse click | Primary actions |
<Button-3> |
Right mouse click | Context menus |
<Enter> |
Mouse enters widget | Hover effects |
<Leave> |
Mouse leaves widget | Reset hover effects |
Conclusion
Mouse position tracking in Tkinter is accomplished using the bind() method with motion events. Use <Motion> for continuous tracking and click events for user interactions. This functionality is essential for creating interactive GUI applications.
