![](/img/trans.png)
[英]Take screenshots at fixed intervals with Python using threading.Timer
[英]trying to make a timer for python screenshots
我正在尝试制作一个脚本,每 30 秒截取一次屏幕截图。
这就是我现在所拥有的:
def on_click(x, y, button, pressed):
global path
if pressed:
takeScreenshoot(path)
print('ScreenShoot Taken')
我试图做什么。
import time
while True: # Change for a variable or a toggle
time.sleep(30)
takeScreenshoot(path)
print('ScreenShoot Taken')
但是现在正因为如此,我的代码的下一部分无法访问
import time
while True: # Change for a variable or a toggle
time.sleep(30)
takeScreenshoot(path)
print('ScreenShoot Taken')
这可能是你可以做的事情,
很简单
import time
while True:
takeScreenshot(path)
time.sleep(30)
print('screenshot taken')
如果你想在一段时间后打破它,这个永远运行的简单 while 代码将起作用,你可以使用索引变量代替。
from threading import Thread
import time
def f(x):
while True:
time.sleep(2.1)
print(f'ScreenShoot Taken: {x}')
def g(x):
for i in range(5):
time.sleep(2.2)
print(f'Ho Ho Ho: {x}')
f = Thread(target=f, args=["bla"])
f.daemon = True
f.start()
g("bla")
在这个例子中,两个函数同时工作。 您当然可以用您的代码替换 g() 。 如果不设置线程为daemon
,程序不会退出,因为线程function f()会无限运行。 如果设置为daemon,线程会在主代码退出的瞬间退出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.