![](/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.