显示如下错误信息。我该如何解决这个问题?
Traceback (most recent call last):
File "c:UsersjayjeotempCodeRunnerFile.py", line 3, in <module>
x, y = pyautogui.locateCenterOnScreen('yellow.png', confidence=0.8)
TypeError: cannot unpack non-iterable NoneType object
我编写了如下代码。我认为如果x.size == 0:是问题。
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)
当我执行print(x)时,我得到相同的错误消息。
这在0.9.41版更改。在此之后,如果没有找到窗口,则会引发异常。在此之前,它返回None
。所以,你需要:
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)
如果你升级,你将不得不为这种情况添加异常处理。