如果我想查找图像,即使不是在那一刻,而是何时找到它,我可以使用什么代码?我的代码是:
import pyautogui as auto
import time
import pyautogui
def Saludar (seconds,schedule) :
while (1>0) and (schedule == True) :
time.sleep(seconds)
x, y = auto.locateCenterOnScreen('linea.png', grayscale=True)
auto.moveTo(x,y)
pyautogui.click()
print("YA")
if __name__== "__main__":
Saludar(2,True)
错误:
Traceback (most recent call last):
File "C:/Users/mario/Desktop/buscar aun si no está.py", line 19, in <module>
Saludar(2,True)
File "C:/Users/mario/Desktop/buscar aun si no está.py", line 11, in Saludar
x, y = auto.locateCenterOnScreen('linea.png', grayscale=True)
TypeError: 'NoneType' object is not iterable
答案 0 :(得分:0)
未找到图像时,将引发TypeError
异常。您可以使用try/except来解决此问题:
import pyautogui as auto
import time
import pyautogui
def Saludar(seconds, schedule):
while (1 > 0) and (schedule == True):
time.sleep(seconds)
try:
x, y = auto.locateCenterOnScreen(
'Desktop/linea.png', grayscale=True)
print('Found it!')
auto.moveTo(x, y)
pyautogui.click()
except TypeError:
"""
Image is not found
"""
print("Image is Not found on the screen!")
if __name__ == "__main__":
Saludar(2, True)