Python: Exit script when it sits Idle after a period of time

huangapple go评论66阅读模式
英文:

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(&quot;C:\\Users\\ul340\\Desktop\\enum_button_image.PNG&quot;, confidence=0.8)
    if not res:
        if count==0:
            start = time.perf_counter()
            count += 1
        else:
            if time.perf_counter()-start&gt;1800:
                break
    else:
        pyautogui.moveTo(res)
        pyautogui.click()
        time.sleep(2)

huangapple
  • 本文由 发表于 2023年5月15日 10:31:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定