我正在嘗試制作一個每 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')
但是現在正因為如此,我的代碼的下一部分無法訪問
uj5u.com熱心網友回復:
import time
while True: # Change for a variable or a toggle
time.sleep(30)
takeScreenshoot(path)
print('ScreenShoot Taken')
這可能是你可以做的,
uj5u.com熱心網友回復:
嗯,這很簡單
import time
while True:
takeScreenshot(path)
time.sleep(30)
print('screenshot taken')
如果您想在一段時間后中斷它,那么這個永遠運行的簡單 while 代碼將起作用,您可以改用索引變數。
uj5u.com熱心網友回復:
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,程式將不會退出,因為執行緒函式 f() 會無限運行。如果設定為守護行程,則執行緒將在主代碼退出的那一刻退出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/468178.html
下一篇:Javascript回圈操作
