1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
"""A `wx.Timer` subclass that invokes a specified callback
periodically.
"""
# Author: Prabhu Ramachandran <prabhu@aero.iitb.ac.in>
# Copyright (c) 2006-2007, Enthought, Inc.
# License: BSD Style.
# Standard library imports.
import wx
######################################################################
# `Timer` class.
class Timer(wx.Timer):
"""Simple subclass of wx.Timer that allows the user to have a
function called periodically.
Any exceptions raised in the callable are caught. If
`StopIteration` is raised the timer stops. If other exceptions are
encountered the timer is stopped and the exception re-raised.
"""
def __init__(self, millisecs, callable, *args, **kw_args):
"""Initialize instance to invoke the given `callable` with
given arguments and keyword args after every `millisecs`
(milliseconds).
"""
wx.Timer.__init__(self, id=wx.NewId())
self.callable = callable
self.args = args
self.kw_args = kw_args
self.Start(millisecs)
def Notify(self):
"""Overridden to call the given callable. Exceptions raised
in the callable are caught. If `StopIteration` is raised the
timer stops. If other exceptions are encountered the timer is
stopped and the exception re-raised.
"""
try:
self.callable(*self.args, **self.kw_args)
except StopIteration:
self.Stop()
except:
self.Stop()
raise
|