【问题标题】:PyautoGUI doesn't give out coordinates for one function while works for anotherPyautoGUI 不为一个函数提供坐标,而为另一个函数工作
【发布时间】:2021-08-02 08:52:55
【问题描述】:

我正在编写一个自动接受英雄联盟游戏的脚本,然后移动到聊天对话以呼叫车道。这对于 click_accept() 函数工作得很好,完美地打印出 x,y 坐标。但是click_chat_dialog()print(pos.x/2,pos.y/2) 行给了我以下错误:

AttributeError: 'NoneType' object has no attribute 'x'

这是我的代码:

import pyautogui, time, os, logging, sys, random, copy

def imPath(filename):
    return os.path.join('images', filename)


def click_accept():
    if pyautogui.locateOnScreen(imPath('accept_button.png'), confidence=0.8) != None:
        pos = pyautogui.locateCenterOnScreen(imPath('accept_button.png'))
        print(pos.x/2,pos.y/2)
        pyautogui.moveTo(pos.x/2,pos.y/2)
        time.sleep(0.5)
        pyautogui.click(pos.x/2,pos.y/2)

def click_chat_dialog():
    if pyautogui.locateOnScreen(imPath('chat_dialog.png'), confidence=0.6) != None:
        pos = pyautogui.locateCenterOnScreen(imPath('chat_dialog.png'))
        print(pos.x/2,pos.y/2)
        pyautogui.moveTo(pos.x/2,pos.y/2)
        time.sleep(0.5)
        pyautogui.click(pos.x/2,pos.y/2)



while 1:
    click_accept()
    click_chat_dialog()

这是我正在使用的图片:

接受按钮 - https://i.imgur.com/1vW6cp3.png

聊天对话 - https://i.imgur.com/PwdXii3.png

接受屏幕 - https://i.imgur.com/xOeLTmq.png

聊天屏幕 - https://i.imgur.com/LfZQCnR.png

【问题讨论】:

    标签: python automation scripting pyautogui


    【解决方案1】:

    当 PyAutoGUI 找不到您要查找的内容时,将返回 NoneType。我看到您已测试是否可以找到该对象,但可能需要对 locatecenter 进行另一个异常处理......没有看到您正在寻找的实际输入(图像),这与我所说的差不多。

    import pyautogui, time, os, logging, sys, random, copy
    
    def imPath(filename):
        return os.path.join('images', filename)
    
    
    def click_accept():
        if pyautogui.locateOnScreen(imPath('accept_button.png'), confidence=0.8) != None:
            pos = pyautogui.locateCenterOnScreen(imPath('accept_button.png'))
            print(pos.x/2,pos.y/2)
            pyautogui.moveTo(pos.x/2,pos.y/2)
            time.sleep(0.5)
            pyautogui.click(pos.x/2,pos.y/2)
    
    def click_chat_dialog():
        if pyautogui.locateOnScreen(imPath('chat_dialog.png'), confidence=0.6) != None:
            try:
                pos = pyautogui.locateCenterOnScreen(imPath('chat_dialog.png'))
            except TypeError:
                #perform some other action because pos was not found..
            print(pos.x/2,pos.y/2)
            pyautogui.moveTo(pos.x/2,pos.y/2)
            time.sleep(0.5)
            pyautogui.click(pos.x/2,pos.y/2)
    
    
    
    while 1:
        click_accept()
        click_chat_dialog()
    

    【讨论】:

    • 感谢您的回答,我的问题中确实有图片链接。虽然接受按钮在图像接受屏幕上完美地给出了坐标,但当我尝试在聊天屏幕图像上获取聊天对话的坐标时,问题就出现了。我可以确认python能够在聊天屏幕图像上找到聊天对话,但仍然弹出坐标为无。
    • 不错的项目。我想知道:聊天对话图像是否足够详细,以至于当适合它的算法找到它的目标时,它会有足够高的信心?我认为您正在尝试最小化图像以便您可以单击它,但也许您可以将聊天对话框的图像放大(不包括上面独特的“XXX 加入大厅”图像?这个概念类似于尝试定向一个圆与定向一个非常可定向的对象(如不等边三角形)。而不是 pos.x/y pos.x/y-一些偏移量以降落在正确的位置?
    • 感谢您的输入,是的,我认为这可能是因为我面临的问题的聊天对话图像的大小,将尝试使用更大的图像。感谢您的意见。
    猜你喜欢
    • 2020-10-07
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 2016-07-21
    • 1970-01-01
    相关资源
    最近更新 更多