- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Mouse Position in Python Tkinter
Events are very useful to perform and manage multiple tasks in a large-scale application. We can bind a particular event with the keyboard buttons or mouse buttons using the bind(‘handler’, ‘callback’) method. Generally, the mouse pointer and its motion are tracked for the purpose of building a screensaver, 2D or 3D games. In order to print the coordinates of the pointer, we have to bind the Motion with a callback function that gets the position of the pointer in x and y variables.
#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") def callback(e): x= e.x y= e.y print("Pointer is currently at %d, %d" %(x,y)) win.bind('<Motion>',callback) win.mainloop()
Running the above code will print the actual position of the pointer whenever we hover on the window.
On the console, you will get to see the actual position of the mouse pointer as you hover the mouse on the screen.
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 Pointer is currently at 376, 159 Pointer is currently at 369, 150 Pointer is currently at 366, 141 Pointer is currently at 362, 130
- Changing the Mouse Cursor in Tkinter
- Binding mouse double click in Tkinter
- Display message when hovering over something with mouse cursor in Tkinter Python
- Setting the position on a button in Tkinter Python?
- How to change the mouse pointer color in Tkinter?
- How get the (x,y) position pointing with mouse in an interactive plot (Python Matplotlib)?
- How to move a Tkinter canvas with Mouse?
- How to hide or disable the mouse pointer in Tkinter?
- How to Change the position of MessageBox using Python Tkinter
- Drawing a line between two mouse clicks using tkinter
- How to draw a line following mouse coordinates with tkinter?
- How to correctly select multiple items with the mouse in Tkinter Treeview?
- Getting the Cursor position in Tkinter Entry widget
- How to configure default Mouse Double-Click behavior in a Tkinter text widget?
- Python Tkinter – How to position a topLevel() widget relative to the root window?