python - 通过屏幕的caputura寻找图像,即使它不在那个时刻
问题描述
如果我想查找图像,我可以使用什么代码,即使它不是在那个时刻,而是在找到它的时候?我的代码是:
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
解决方案
当找不到图像时,它会引发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)
推荐阅读
- pandas - 代码显示错误:nan,它与 np.nan 有关还是其他错误?
- javascript - 我需要在翻转时钟倒计时中添加一个活动类
- node.js - 使用护照和快速会话创建用户帐户登录页面
- webrtc - 如何在 Ant Media Server 中设置 SameSite 属性?
- javascript - 我正在尝试为音轨的左右声道设置增益,以便我可以通过两个扬声器一次播放任一声道
- php - 当我在本地主机上激活 asta 主题时出现致命错误
- frama-c - 出现此错误时我该怎么办?Alt-Ergo:“未知错误”
- java - JPA 实体:无法找到持久性
- html - 如何修改超链接的标题css
- reactjs - 从函数更新道具时更新组件