问题描述
我正在编写一个自动接受英雄联盟游戏的脚本,然后移动到聊天对话以调用车道。这适用于 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()