Last active
December 29, 2015 17:39
-
-
Save blink1073/7705815 to your computer and use it in GitHub Desktop.
Take a snapshot of an enaml application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Fri Nov 29 07:29:42 2013 | |
@author: Steven Silvester <steven.silvester@ieee.org> | |
@license: MIT | |
Take a snapshot of an enaml application. | |
Open an enaml application, save a snapshot, and optionally close it. | |
Requires PyQt4 (PySide not supported). | |
Example usage: | |
import enaml | |
from enaml.qt.qt_application import QtApplication | |
with enaml.imports(): | |
from my_view import Main | |
app = QtApplication() | |
view = Main() | |
snapshot = SnapShot(path='snapshot.png', view=view) | |
view.show() | |
app.start() | |
""" | |
import os | |
os.environ['QT_API'] = 'pyqt' | |
from atom.api import Atom, Unicode, Value, Bool, Int | |
from enaml.qt.QtGui import QApplication, QPixmap | |
from enaml.application import timed_call | |
class SnapShot(Atom): | |
""" Generate a snapshot of an enaml view. | |
""" | |
#: The snapshot save path. | |
path = Unicode() | |
#: The enaml view object. | |
view = Value() | |
#: The delay prior to taking a snapshot | |
delay_ms = Int(1000) | |
#: Whether to close after taking the snapshot. | |
auto_close = Bool(True) | |
def _observe_view(self, change): | |
""" Move window and allow it to draw before taking the snapshot. | |
""" | |
if change['type'] == 'create': | |
self.view.initial_position = (10, 10) | |
self.view.always_on_top = True | |
timed_call(self.delay_ms, self.snapshot) | |
def snapshot(self): | |
""" Take a snapshot of the window and optionally close it. | |
""" | |
widget = self.view.proxy.widget | |
framesize = widget.window().frameSize() | |
QPixmap.grabWindow(QApplication.desktop().winId(), widget.x(), | |
widget.y(), framesize.width(), | |
framesize.height() ).save(self.path) | |
if self.auto_close: | |
widget.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment