英文:
Python: Exit script when it sits Idle after a period of time
问题
I'm required to click a button when it appears onscreen over and over again to copy some files.
So I created a Python program that automatically clicks this button whenever it appears on screen until it's finished. I screenshotted a PNG image of the button the enum_buttom_image.png
file.
Here's my code:
import pyautogui
import time
while True:
res = pyautogui.locateCenterOnScreen("C:\\Users\\ul340\\Desktop\\enum_button_image.PNG", confidence=0.8)
pyautogui.moveTo(res)
pyautogui.click()
time.sleep(2)
Right now I'm terminating the program manually once it finished (CTRL+C) on the command prompt window. I wanted to know how could I modify this program so it automatically exits the script once it doesn't find the button image for more than 30 minutes?
英文:
I'm required to click a button when it appears onscreen over and over again to copy some files.
So I created a Python program that automatically clicks this button whenever it appears on screen until it's finished.
I screenshotted a PNG image of the button the enum_buttom_image.png
file.
Here's my code:
import pyautogui
import time
while True:
res = pyautogui.locateCenterOnScreen("C:\\Users\\ul340\\Desktop\\enum_button_image.PNG", confidence=0.8)
pyautogui.moveTo(res)
pyautogui.click()
time.sleep(2)
Right now I'm terminating the program manually once it finished (CTRL+C) on the command prompt window. I wanted to know how could I modify this program so it automatically exit the script once it doesn't find the button image for more than 30 minutes?
答案1
得分: 1
res
在未定位到图像时为None
,因此可以用这种方式来检查时间限制:
import pyautogui
import time
count=0
while True:
res = pyautogui.locateCenterOnScreen("C:\\Users\\ul340\\Desktop\\enum_button_image.PNG", confidence=0.8)
if not res:
if count==0:
start = time.perf_counter()
count += 1
else:
if time.perf_counter()-start>1800:
break
else:
pyautogui.moveTo(res)
pyautogui.click()
time.sleep(2)
希望这对你有帮助。
英文:
res
is None
when image is not located so,<br>
This can be a way to check the time constraint:
import pyautogui
import time
count=0
while True:
res = pyautogui.locateCenterOnScreen("C:\\Users\\ul340\\Desktop\\enum_button_image.PNG", confidence=0.8)
if not res:
if count==0:
start = time.perf_counter()
count += 1
else:
if time.perf_counter()-start>1800:
break
else:
pyautogui.moveTo(res)
pyautogui.click()
time.sleep(2)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论