Python:脚本闲置一段时间后退出

问题描述 投票:0回答:1

当一个按钮出现在屏幕上时,我需要点击它来复制一些文件。

所以我创建了一个 Python 程序,只要它出现在屏幕上,它就会自动点击这个按钮,直到它完成。 我截取了按钮

the enum_buttom_image.png
文件的PNG图像。

这是我的代码:

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)

现在我在命令提示符窗口中完成 (CTRL+C) 后手动终止程序。我想知道如何修改这个程序,让它在超过 30 分钟后找不到按钮图像时自动退出脚本?

python pyautogui
1个回答
0
投票

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)
© www.soinside.com 2019 - 2024. All rights reserved.