问题描述
所以我希望程序主动寻找我在屏幕上的某个区域提供的图像。如果代码在屏幕上,我想运行代码,如果不在屏幕上,则运行其他代码
我一直在尝试使用 pyautogui 执行此操作,但似乎不起作用。
pyautogui.locateCenterOnScreen('F.png',confidence = .7,region = (275,259,1011,482))
当我打印这个但没有找到图像时,输出是:无 当我打印它并找到它会输出的图像时:Point(x=297,y=266)
所以我设置了一个 if 语句
findImg = pyautogui.locateCenterOnScreen('F.png',482))
print (findImg)
if findImg == 'None':
print ("not found")
这会打印无但不打印未找到
解决方法
发生这种情况是因为 'None'
在引号中,所以它是一个字符串,而不是 None
。试试if findImg == None:
。虽然更好的方法可能是将 print(findImg)
移动到 else:
块,这样它就不会打印“None”和“not found”
if findImg == None:
print ("not found")
else:
print(findImg)