Silly me -- it's really easy, you don't even need a GUI running.
import Tkinter as tk root = tk.Tk() root.winfo_pointerx() # this returns the absolute mouse x co - ordinate.
Try the below sample code it will help to get you a better understanding
import Tkinter as tk
import Xlib.display as display
def mousepos(screenroot = display.Display().screen().root):
pointer = screenroot.query_pointer()
data = pointer._data
return data["root_x"], data["root_y"]
def update():
strl.set("mouse at {0}".format(mousepos()))
root.after(100, update)
root = tk.Tk()
strl = tk.StringVar()
lab = tk.Label(root, textvariable = strl)
lab.pack()
root.after(100, update)
root.title("Mouseposition")
root.mainloop()
Last Updated : 21 Jan, 2021,GATE CS 2021 Syllabus
pip3 install pyautogui
Save this file with .py extension, and then run the file.
This python code use size() function to output your screen resolution in x, y format:
Output:
(1920, 1080)
The pyautogui.moveTo() function will move the mouse cursor to the specific location on the screen. The duration=1 sets out the speed in which the mouse cursor is moving.,The pyautogui.move() function will move the mouse cursor to the relative position where the mouse cursor is currently located at.,The pyautogui.click() function will simulate the clicking of the mouse at the specific (x,y) coordinates.,We can use the pyautogui.position() function to retrieve the current position of mouse cursor.
PyAutoGui is a Python module for automation with the Graphical User Interface (GUI). It can be used to programmatically control the mouse cursor. To install just run the following command in the terminal.
pip install pyautogui
By using the pyautogui.size()
function, we can determine the screen resolution of the user running the python program, this allow us to determine the max position the mouse cursor can travel.
import pyautogui
width_height = pyautogui.size()
print(width_height)
print(width_height[0])
print(width_height[1])
output
Size(width = 1920, height = 1080) 1920 1080
The pyautogui.move()
function will move the mouse cursor to the relative position where the mouse cursor is currently located at.
import pyautogui # move to relative position of current mouse cursor pyautogui.move(100, 0, duration = 1) pyautogui.move(0, 100, duration = 1) pyautogui.move(-100, 0, duration = 1) pyautogui.move(0, -100, duration = 1)
We can use the pyautogui.position()
function to retrieve the current position of mouse cursor.
import pyautogui
position = pyautogui.position()
print(position)
print(position[0])
print(position[1])