我试图用库pyautogui
检测一个按钮,其思想是,当检测到按钮时,只需打印迭代试验,当最终检测到按钮时,只需使用pyautogui.locateOnScreen()
打印按钮的位置并中断迭代
但是,下一个代码将永远停留在计算第一次迭代(0
)时,并且永远不会计算随后的if
条件:
import pyautogui
import os
def button_detector():
for i in range(450):
some_button=pyautogui.locateOnScreen(os.path.join(ROOT_DIR, r'some_button.png'), region=(0, 510, 547, 153) , grayscale=False)
while some_button is None:
print('Button not found at trial {}'.format(i))
print('Error: did not find any button')
if some_button is not None:
print(some_button)
print("Button detected")
break
如何打破while条件以完成此任务
还有其他更简单的方法吗
我不知道为什么要使用
for
和while
,但对我来说,它应该是if/else
而不是while
和嵌套的if
但对我来说,它需要的是
return some_button
而不是break
或者使用更少的
prints
和sleep
,因为代码可能运行得太快编辑:
我检查了locateOnScreen的源代码,它使用了函数 pyscreeze.locateOnScreen有选项
minSearchTime
,所以如果您想等待按钮的时间更长,例如3秒,那么您可以使用它将运行一个循环,这个循环将反复检查它3秒钟,而您不必创建自己的循环
或者更简单
导入pyautogui 导入操作系统
相关问题 更多 >
编程相关推荐