使用threading.Timer以固定间隔与Python进行屏幕截图
我正在用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.main()被调用,如果你想要线程与gtk
让_takeScreenshot()有一个while循环,而不要为下一个屏幕快照启动一个新线程。 你已经有一个工作线程,例如
您还需要一个线程而不是定时器
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
链接地址: http://www.djcxy.com/p/52581.html
上一篇: Take screenshots at fixed intervals with Python using threading.Timer
下一篇: Can screen readers for the blind handle tabbed browsing?