未找到异常图像

2024-10-04 01:36:55 发布

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

我正在创建一个脚本来识别一个图像并执行一些操作,基本上当我发现图像是一个操作时,当我找不到时,就执行另一个操作。 但是当找不到映像时,pyautogui本身已经返回错误并关闭函数。返回以下错误:ImageNotFoundExeption('找不到图像….') 我基本上需要它来工作

print('Press Ctrl-C to quit.')
try:
    while True:

        def locate_img(img):
            locate = pyautogui.locateOnScreen(img)

            if locate:
                X,Y = pyautogui.center(locate)                              
                pyautogui.moveTo(X,Y, 0.2)

            else:
                X,Y = pyautogui.center(locate)                              
                pyautogui.moveTo(X,Y, 0.2)  


        locate_img(find)

except KeyboardInterrupt:
    print('\nDone.')

如果图像未定位,则会返回locate中的直接错误,甚至不会转到else


Tags: to函数图像脚本img错误elselocate
2条回答

也许你会想要在try-except块中抛出错误的实际部分,而不是全部。你知道吗

while True:

    def locate_img(img):

        try:
             locate = pyautogui.locateOnScreen(img)
        except KeyboardInterrupt:
             locate = None

        if locate:
            X,Y = pyautogui.center(locate)                              
            pyautogui.moveTo(X,Y, 0.2)

        else:
            X,Y = pyautogui.center(locate)                              
            pyautogui.moveTo(X,Y, 0.2)  


    locate_img(find)

您可以使用许多,除非egx需要:

except KeyboardInterrupt:
    print('Done')
except Exception as e:
    print(e)

例外情况充当通配符

相关问题 更多 >