pyautogui.locateCenterOnScreen
のドキュメントはこちらにあります。
https://pyautogui.readthedocs.io/en/latest/screenshot.html#the-locate-functions
これをみると、見つからない場合は ImageNotFoundException
を投げるという仕様になっています。
したがって、例外を用いない方法は不可能です。
コメントを受けての追記: 例外の補足の仕方
ImageNotFoundException
は pyscreeze
で定義されていますので、次のように import してから使ってください。
from pyscreeze import ImageNotFoundException
try:
locate = pg.locateCenterOnScreen('search3.png')
pg.click(locate)
except ImageNotFoundException:
pg.alert(text='探索失敗', button='OK')
コメントを受けての追記: pyscreeze の設定変更の仕方
pyautogui
の内部で使われている pyscreeze
が2度仕様変更しましたが、pyautogui
が2度目の仕様変更に追従していないようです。
このため、pyscreeze
が2度目の仕様変更の前の振る舞いをするように、以下のように設定してやる必要があります。
import pyautogui as pg
from pyscreeze import ImageNotFoundException
# pyscreeze の設定
import pyscreeze
pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = True
try:
locate = pg.locateCenterOnScreen('search3.png')
pg.click(locate)
except ImageNotFoundException:
pg.alert(text='探索失敗', button='OK')
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。