如何让 pyautogui 告诉我 locateOnScreen 函数的坐标
How can I make pyautogui tell me the coordinates of the locateOnScreen function
我现在的目标是能够从 pyautogui
函数 locateOnScreen
接收坐标,并使用返回的坐标单击屏幕上的对象。我知道如何使用坐标点击屏幕,但我无法从 locateOnScreen
函数
中找到坐标
代码
这就是我目前所拥有的,它可以在屏幕上找到一个对象并确定该对象是否可见。我只需要抓取对象的坐标即可。
from pyautogui import *
import pyautogui
import time
import keyboard
import random
while True:
if pyautogui.locateOnScreen('x1.png', confidence=0.9) is not None:
print("I can see it")
time.sleep(1)
else:
print("I can not see the X")
time.sleep(2)
x1.png
参见the documentation:locateOnScreen
函数的return值保存坐标。
x1_coordinates = pyautogui.locateOnScreen('x1.png', confidence=0.9)
print(x1_coordinates) # This will print out where it is
if x1_coordinates:
print(f"I can see it at {x1_coordinates}")
else:
print("I cannot see it.")
更新:在聊天中,我们还讨论了单击该对象。为此,您可以使用 locateCenterOnSCreen
或 pyautogui.click(x.left + 3, x.top - 3)
(这有点麻烦,我不建议使用它)。
使用locateCenterOnScreen得到x
和y
坐标:
import pyautogui
while True:
try:
x, y = pyautogui.locateCenterOnScreen('x1.png')
print(f'I can see it at: {x}x{y}')
except TypeError:
print('I can not see it')
输出:
I can not see it
I can see it at: 960x571
我现在的目标是能够从 pyautogui
函数 locateOnScreen
接收坐标,并使用返回的坐标单击屏幕上的对象。我知道如何使用坐标点击屏幕,但我无法从 locateOnScreen
函数
代码
这就是我目前所拥有的,它可以在屏幕上找到一个对象并确定该对象是否可见。我只需要抓取对象的坐标即可。
from pyautogui import *
import pyautogui
import time
import keyboard
import random
while True:
if pyautogui.locateOnScreen('x1.png', confidence=0.9) is not None:
print("I can see it")
time.sleep(1)
else:
print("I can not see the X")
time.sleep(2)
x1.png
参见the documentation:locateOnScreen
函数的return值保存坐标。
x1_coordinates = pyautogui.locateOnScreen('x1.png', confidence=0.9)
print(x1_coordinates) # This will print out where it is
if x1_coordinates:
print(f"I can see it at {x1_coordinates}")
else:
print("I cannot see it.")
更新:在聊天中,我们还讨论了单击该对象。为此,您可以使用 locateCenterOnSCreen
或 pyautogui.click(x.left + 3, x.top - 3)
(这有点麻烦,我不建议使用它)。
使用locateCenterOnScreen得到x
和y
坐标:
import pyautogui
while True:
try:
x, y = pyautogui.locateCenterOnScreen('x1.png')
print(f'I can see it at: {x}x{y}')
except TypeError:
print('I can not see it')
输出:
I can not see it
I can see it at: 960x571