【问题标题】:How can I make pyautogui tell me the coordinates of the locateOnScreen function如何让 pyautogui 告诉我 locateOnScreen 函数的坐标
【发布时间】:2021-02-24 02:13:19
【问题描述】:

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

【问题讨论】:

    标签: python automation pyautogui


    【解决方案1】:

    参见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)(这有点小技巧,我不建议使用它)。

    【讨论】:

    • 所以我这样做了,但点击关闭我不知道为什么from pyautogui import * import pyautogui import time import keyboard import random while True: 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}") pyautogui.click(x1_coordinates) else: print("I cannot see it.")
    • “点击已关闭”是什么意思?你能说得更准确一些吗?或许可以提供更多细节说明什么是行不通的?
    • 喜欢它没有在正确的位置点击,抱歉它似乎没有正确格式化代码
    • 一切顺利。尝试在 X 和 Y 坐标上添加/减去 2-3 个像素以命中正确的位置,或者改用 locateCenterOnScreen。
    • 当然!如果它对你有用,请务必接受这个答案并告诉我什么有效 - 我会为未来的 Google 员工相应地编辑我的答案:)
    【解决方案2】:

    使用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
    

    【讨论】:

    • 它似乎不是 persizes 如何从坐标中减去像素
    • 使用pyautogui.click(x, y)点击按钮
    • 但它不是 persize 所以它没有点击正确的位置我该如何解决这个问题
    • 它确实点击了按钮。你要点击什么?
    • 我希望它点击我屏幕上的 x 按钮,但它完全关闭了我即将创建另一个问题并提供更好解释的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 2020-07-27
    • 2016-05-10
    相关资源
    最近更新 更多