[英]Take screenshots at fixed intervals with Python using threading.Timer
我正在用pyGTK編寫一個簡單的GUI應用程序,以固定的間隔拍攝桌面截圖。 為了安排鏡頭我使用threading.Timer類並拍攝我使用os.system調用scrot。
當我單擊開始截屏按鈕時,將調用GlapseMain.startScreenshots方法。 當我單擊停止截屏按鈕時,將調用GlapseMain.stopScreenshots方法。
事情是在GTK應用程序運行時,雖然它應該沒有截屏。 當我點擊關閉按鈕時,它會開始無限期地截取屏幕截圖。
這是我的代碼:
#!/usr/bin/env python
# -*- coding: utf8 -*-
import threading
import os
class GlapseMain:
def __init__(self):
self.outputDir = os.getenv('HOME')
self.quality = 80
self.interval = 10
self.numDigits = 15
self.currentShot = 0
def startScreenshots(self, output, quality, interval):
print 'Starting taking screenshots...'
print 'Output folder: ' + str(output)
print 'Quality: ' + str(quality)
print 'Interval: ' + str(interval)
# Update attributes
self.outputDir = output
self.quality = quality
self.interval = interval
self.currentShot = 0
# Create timer (first screenshot scheduled 1s ahead)
self.timer = threading.Timer(1.0, self._takeScreenshot)
self.timer.start()
def stopScreenshots(self):
print 'Stopped taking screenshots.'
self.timer.cancel()
def _takeScreenshot(self):
# Build scrot command
fileNumber = str(self.currentShot)
fileNumber = fileNumber.zfill(self.numDigits - len(fileNumber))
fileName = 'scr-' + fileNumber + '.jpg'
command = 'scrot -q ' + str(self.quality) + ' ' + self.outputDir + os.sep + fileName
print 'Taking screenshot: ' + command + '...'
os.system(command)
# Schedule next screenshot
self.currentShot = self.currentShot + 1
self.timer = threading.Timer(self.interval, self._takeScreenshot)
self.timer.start()
我的輸出看起來像這樣:
Starting taking screenshots...
Output folder: /home/david/glapse-screens
Quality: 80.0
Interval: 2.0
Stopped taking screenshots.
Taking screenshot: scrot -q 80.0 /home/david/glapse-screens/scr-00000000000000.jpg...
Closing gLapse...
Taking screenshot: scrot -q 80.0 /home/david/glapse-screens/scr-00000000000001.jpg...
Taking screenshot: scrot -q 80.0 /home/david/glapse-screens/scr-00000000000002.jpg...
Taking screenshot: scrot -q 80.0 /home/david/glapse-screens/scr-00000000000003.jpg...
Taking screenshot: scrot -q 80.0 /home/david/glapse-screens/scr-00000000000004.jpg...
Taking screenshot: scrot -q 80.0 /home/david/glapse-screens/scr-00000000000005.jpg...
Taking screenshot: scrot -q 80.0 /home/david/glapse-screens/scr-00000000000006.jpg...
Taking screenshot: scrot -q 80.0 /home/david/glapse-screens/scr-00000000000007.jpg...
希望你能幫助我,非常感謝你。
編輯:
我已經改變了方法,現在我正在使用線程:
def startScreenshots(self, output, quality, interval):
print 'Starting taking screenshots...'
print 'Output folder: ' + str(output)
print 'Quality: ' + str(quality)
print 'Interval: ' + str(interval)
# Update attributes
self.outputDir = output
self.quality = quality
self.interval = interval
self.currentShot = 0
# Start a thread to take screenshots
self.done = False
self.thread = threading.Thread(target=self._takeScreenshot)
self.thread.start()
def stopScreenshots(self):
print 'Stopped taking screenshots.'
self.done = True
self.thread.join()
def _takeScreenshot(self):
# Run until we're done
while not self.done:
# Build scrot command
fileNumber = str(self.currentShot)
fileNumber = fileNumber.zfill(self.numDigits - len(fileNumber))
fileName = 'scr-' + fileNumber + '.jpg'
command = 'scrot -q ' + str(self.quality) + ' ' + self.outputDir + os.sep + fileName
print 'Taking screenshot: ' + command + '...'
os.system(command)
# Schedule next screenshot
print 'Scheduling next screenshot...'
self.currentShot = self.currentShot + 1
time.sleep(self.interval)
它打印消息但不執行os.system指令直到我按下停止按鈕。
回應:
你還需要打電話
gtk.gdk.threads_init()
如果你想要帶有gtk的線程,則在調用gtk.main()之前
讓_takeScreenshot()改為使用while循環,不要為下一個屏幕截圖啟動新線程。 你已經有了一個工作線程,例如
您還需要一個Thread而不是Timer
def _takeScreenshot(self):
while self.notDone:
# take screen shot
# ..
time.sleep(self.interval)
然后在你的取消方法做類似的事情:
def stopScreenshots(self):
self.notDone = False
self.timer.join() # wait for thread to finish
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.