I'm running linux (Pop OS 22.04) and I'm trying to make a loop using pyautgui.locateOnScreen method, here's the code:
def __clicks_print(area, *print_names, nclicks=1):
loc = None
while not loc:
for print_name in print_names:
loc = pyautogui.locateOnScreen(print_name, region=area)
print(loc)
if loc:
break
pyautogui.moveTo(loc)
pyautogui.click(clicks=nclicks)
When I try to run the function the program ends-up stopping with the error below:
raise ImageNotFoundException # Raise PyAutoGUI's ImageNotFoundException.
The strange part is that I use this exact type of loop in Windows and it just works, the program keeps looking for a loc until it finds one.
I tried to copy the exact same function I have on a Windows machine and, it doesn't work, the program keeps stopping when it fails to locate the pixels. I want the program to keep looking until it finds the pixels on the screen. This way I can give the function multiple prints to locate.
I managed to make it work by using a try except, so I can ignore the exception. It seems that in older pyautogui versions it was returning "None" when the image was not found and now, in more recent versions, it is throwing an excepetion. The code:
def __clicks_print(area, *print_names, nclicks=1):
loc = None
while not loc:
for print_name in print_names:
try:
loc = pyautogui.locateOnScreen(print_name, region=area)
if loc:
break
except:
pass
pyautogui.moveTo(loc)
pyautogui.click(clicks=nclicks)