Get X and Y from the screen using PYTHON

Asked

Viewed 4,130 times

3

I want to create a bot(macro) that performs some click tasks, for this I am using the autopygui in python!

But I don’t have much experience in X and Y, and testing it manually and boring.

I would like, to get the x and y by clicking on the screen, for example, I click on such screen location and present on the console the X and Y of the click location. Possible?

3 answers

3


Good a way to do this in autopygui would basically be like this:

#pega o retorno da posicao atual de x e y do mouse e passa o valor da tupla para as duas variaveis
x, y = pyautogui.position()
print "Posicao atual do mouse:"
print "x = "+str(x)+" y = "+str(y)

#retorna True se x & y estiverem dentro da tela
print "\nEsta dentro da tela?"
resp = pyautogui.onScreen(x, y)
print str(resp)

Reference: autopygui

1

A quick solution using Pyuserinput and without going into specifics of operating system: instances of mouse have the method position(), which returns the position (X, Y) at each call:

$ pip install git+https://github.com/PyUserInput/PyUserInput.git
>>> import pymouse
>>> mouse = pymouse.PyMouse()
>>> mouse.position()
(562, 528)
>>> mouse.position()
(1259, 195)
>>> mouse.position()
(157, 259)

-2

It took me a long time to ask you the most I edited the above code for users. Just copy and play in IDLE or IDE.

code:

import pyautogui
import time
#pega o retorno da posicao atual de x e y do mouse e passa o valor da tupla para as duas variaveis
print('Posicione o MOUSE')
time.sleep(2)
x, y = pyautogui.position()
print ("Posicao atual do mouse:")
print ("x = "+str(x)+" y = "+str(y))

#retorna Truee se x & y estiverem dentro da tela
print ("\nEsta dentro da tela?")
resp = pyautogui.onScreen(x, y)
print (str(resp))
  • 1

    I didn’t understand the need to copy an answer just to add one print and parentheses in the others prints

Browser other questions tagged

You are not signed in. Login or sign up in order to post.