微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我怎样才能让 pyautogui 告诉我 locateOnScreen 函数的坐标

如何解决我怎样才能让 pyautogui 告诉我 locateOnScreen 函数的坐标

我现在的目标是能够从 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

x1.png

解决方法

参见the documentationlocateOnScreen 函数的返回值保存坐标。

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.")

更新:在聊天中,我们还讨论了点击该对象。为此,您可以使用 locateCenterOnSCreenpyautogui.click(x.left + 3,x.top - 3)(这有点小技巧,我不建议使用它)。

,

使用 locateCenterOnScreen 获取 xy 坐标:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。