代码之家  ›  专栏  ›  技术社区  ›  mmewtwosaysbye

为什么pyautogui说“ImageNotFoundException”而不是“没有什么”?

  •  2
  • mmewtwosaysbye  · 技术社区  · 2 年前

    尝试制作图像识别程序。代码:

    from pyautogui import *
    import pyautogui
    import time
    import keyboard
    import random
    import win32api, win32con
    
    while 1:
        if pyautogui.locateOnScreen('Dassault Falcon 7X.png', confidence=0.8,minSearchTime=5) != None:
            print ("I can see it!")
            time.sleep(0.5)
        else:
            print ("Nope nothing there")
    
    

    出于某种原因,它总是给我红色的imagenotfounderexception,而不是我想让它在什么都没找到时说的“没有,什么都没有。”

    1 回复  |  直到 2 年前
        1
  •  1
  •   Anerdw    2 年前

    从 pyautogui 文档:

    注意:从0.9.41版本开始,如果locate函数找不到提供的映像,它们将引发ImageNotFoundException,而不是返回None。

    您的程序假定0.9.41之前的行为。要将其更新为最新版本,请将if-else块替换为try-excel:

    from pyautogui import locateOnScreen, ImageNotFoundException
    
    while True:
        try:
            pyautogui.locateOnScreen('Dassault Falcon 7X.png')
            print("I can see it!")
            time.sleep(0.5)
        except ImageNotFoundException:
            print("Nope nothing there")