Python 自信地单击图像

Python 自信地单击图像,python,pyautogui,Python,Pyautogui,我正在尝试为我玩的游戏创建一个宏。我想使用pyautogui找到一个具有置信度=(0.2)的图像,然后将鼠标移动到其位置并单击。我不知道接下来该怎么办。它将找到图像并打印,但当我尝试添加pyautogui时。单击('forgedspirit.png')我得到一个错误:无法解压缩不可编辑的非类型对象 def new_method(): if pyautogui.locateOnScreen('forgespirit.png', confidence=(0.2)) != None:

我正在尝试为我玩的游戏创建一个宏。我想使用pyautogui找到一个具有
置信度=(0.2)
的图像,然后将鼠标移动到其位置并单击。我不知道接下来该怎么办。它将找到图像并打印,但当我尝试添加
pyautogui时。单击('forgedspirit.png')
我得到一个错误:
无法解压缩不可编辑的非类型对象

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

您应该告诉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")

我想问题在于它并不完全匹配;如果发现它的置信度小于1.0,则
pyautogui.click(name)
快捷方式将不起作用,因为它需要精确匹配。为什么不分配
locateOnScreen的结果并使用它(如果您使用的是Python 3.8及更高版本,您可以作为条件的一部分:
如果(image:=pyautogui.locateOnScreen(…)不是None:…
)?我使用的是Python 3.7。我尝试了:forgespirit=pyautogui.locateOnScreen('forgespirit.png',confidence=(0.25))def new_method():而True:如果forgespirit不是None:print(“我能看到它”)pyautogui.click(forgespirit)time.sleep(0.5)else:print(“找不到图像”)time.sleep(0.5)new_method()但现在它找不到图像。