使用threading.Timer,使用Python以固定间隔截取屏幕截图

时间:2011-05-06 16:24:33

标签: python timer gtk pygtk screenshot

我正在用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指令,直到我按下停止按钮。

1 个答案:

答案 0 :(得分:4)

回复:

您还需要致电

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