Home > Software engineering >  pyautogui throws me error message. How can I fix my code?
pyautogui throws me error message. How can I fix my code?

Time:11-21

It gives me error message like below. How can I fix this?

Traceback (most recent call last):
  File "c:\Users\jayjeo\tempCodeRunnerFile.py", line 3, in <module>
    x, y = pyautogui.locateCenterOnScreen('yellow.png', confidence=0.8)
TypeError: cannot unpack non-iterable NoneType object

I made a code as below. I think that if x.size == 0: is the problem.

import pyautogui

x, y = pyautogui.locateCenterOnScreen('yellow.png', confidence=0.8)
if x.size == 0:
    print("Not Detected")
    pyautogui.click(1280,720)
else:
    print("Detected")
    pyautogui.click(x, y)

When I do print(x), I get the same error message.

CodePudding user response:

This was changed in version 0.9.41. After that point, if the window is not found, it raises an exception. Before that point, it returns None. So, you need:

pt = pyautogui.locateCenterOnScreen('yellow.png', confidence=0.8)
if not pt:
    print("Not Detected")
    pyautogui.click(1280,720)
else:
    x, y = pt
    print("Detected")
    pyautogui.click(x, y)

If you upgrade, you will have to add exception handling for this case.

  • Related