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

PyautoGUI 在为另一个函数工作时不会给出一个函数的坐标

如何解决PyautoGUI 在为另一个函数工作时不会给出一个函数的坐标

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

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)



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

解决方法

当 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)



while 1:
    click_accept()
    click_chat_dialog()

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