自信地单击图像

2024-10-04 05:34:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我玩的游戏创建一个宏。我想使用pyautogui查找带有confidence=(0.2)的图像,然后将鼠标移动到其位置并单击。我不知道接下来该怎么办。它将找到图像并打印,但当我尝试添加pyautogui.click('forgedspirit.png')时,我得到一个错误:cannot unpack non-iterable NoneType object

def new_method():  
    if pyautogui.locateOnScreen('forgespirit.png', confidence=(0.2)) != None:
        print("I can see it.")
        pyautogui.click('forgespirit.png')
        time.sleep(0.5)
    else:
        print("image not found")
        time.sleep(0.5)
new_method()

Tags: 图像游戏newtimepng错误sleep鼠标
1条回答
网友
1楼 · 发布于 2024-10-04 05:34:41

您应该告诉pyautogui单击图像的坐标,而不是让它单击图像。 试试这个:

try:
    pos = pyautogui.locateOnScreen('forgespirit.png', confidence=(0.2))
    if pos != None:
        print("I can see it.")
        pyautogui.click(pos[0], pos[1])
        time.sleep(0.5)
    else:
        print("image not found")
        time.sleep(0.5)
except:
    print("image not found")

相关问题 更多 >