Revision: 8260 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8260&view=rev Author: ryanmay Date: 2010-04-20 22:37:01 +0000 (Tue, 20 Apr 2010)
Log Message: ----------- Update timer support to allow for kwargs on callbacks. Also fix up a few bugs in the TkAgg implementation. Modified Paths: -------------- trunk/matplotlib/examples/event_handling/timers.py trunk/matplotlib/lib/matplotlib/backend_bases.py trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py trunk/matplotlib/lib/matplotlib/backends/backend_tkagg.py trunk/matplotlib/lib/matplotlib/backends/backend_wx.py Modified: trunk/matplotlib/examples/event_handling/timers.py =================================================================== --- trunk/matplotlib/examples/event_handling/timers.py 2010-04-20 20:23:00 UTC (rev 8259) +++ trunk/matplotlib/examples/event_handling/timers.py 2010-04-20 22:37:01 UTC (rev 8260) @@ -16,8 +16,7 @@ # Create a new timer object. Set the interval 500 milliseconds (1000 is default) # and tell the timer what function should be called. -timer = fig.canvas.new_timer() -timer.interval = 100 +timer = fig.canvas.new_timer(interval=100) timer.add_callback(update_title, ax) timer.start() Modified: trunk/matplotlib/lib/matplotlib/backend_bases.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backend_bases.py 2010-04-20 20:23:00 UTC (rev 8259) +++ trunk/matplotlib/lib/matplotlib/backend_bases.py 2010-04-20 22:37:01 UTC (rev 8260) @@ -893,11 +893,19 @@ upon timer events. This list can be manipulated directly, or the functions add_callback and remove_callback can be used. ''' - def __init__(self): - #Initialize empty callbacks list and setup default settings - self.callbacks = [] + def __init__(self, interval=None, callbacks=None): + #Initialize empty callbacks list and setup default settings if necssary + if callbacks is None: + self.callbacks = [] + else: + self.callbacks = callbacks[:] # Create a copy + + if interval is None: + self._interval = 1000 + else: + self._interval = interval + self._single = False - self._interval = 1000 # Default attribute for holding the GUI-specific timer object self._timer = None @@ -949,21 +957,21 @@ single_shot = property(_get_single_shot, _set_single_shot) - def add_callback(self, func, *args): + def add_callback(self, func, *args, **kwargs): ''' Register `func` to be called by timer when the event fires. Any additional arguments provided will be passed to `func`. ''' - self.callbacks.append((func, args)) + self.callbacks.append((func, args, kwargs)) - def remove_callback(self, func, *args): + def remove_callback(self, func, *args, **kwargs): ''' - Remove `func` from list of callbacks. `args` is optional and used - to distinguish between copies of the same function registered to be - called with different arguments. + Remove `func` from list of callbacks. `args` and `kwargs` are optional + and used to distinguish between copies of the same function registered + to be called with different arguments. ''' - if args: - self.callbacks.remove((func, args)) + if args or kwargs: + self.callbacks.remove((func, args, kwargs)) else: funcs = [c[0] for c in self.callbacks] if func in funcs: @@ -983,10 +991,10 @@ can return False if they should not be called any more. If there are no callbacks, the timer is automatically stopped. ''' - for func,args in self.callbacks: - ret = func(*args) + for func,args,kwargs in self.callbacks: + ret = func(*args, **kwargs) if ret == False: - self.callbacks.remove((func,args)) + self.callbacks.remove((func,args,kwargs)) if len(self.callbacks) == 0: self.stop() @@ -1929,13 +1937,21 @@ """ return self.callbacks.disconnect(cid) - def new_timer(self): + def new_timer(self, *args, **kwargs): """ Creates a new backend-specific subclass of :class:`backend_bases.Timer`. This is useful for getting periodic events through the backend's native event loop. Implemented only for backends with GUIs. + + optional arguments: + + *interval* + Timer interval in milliseconds + *callbacks* + Sequence of (func, args, kwargs) where func(*args, **kwargs) will + be executed by the timer every *interval*. """ - return TimerBase() + return TimerBase(*args, **kwargs) def flush_events(self): """ Modified: trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py 2010-04-20 20:23:00 UTC (rev 8259) +++ trunk/matplotlib/lib/matplotlib/backends/backend_gtk.py 2010-04-20 22:37:01 UTC (rev 8260) @@ -448,13 +448,21 @@ def get_default_filetype(self): return 'png' - def new_timer(self): + def new_timer(self, *args, **kwargs): """ - Creates a new backend-specific subclass of - :class:`backend_bases.TimerBase`. This is useful for getting periodic - events through the backend's native event loop. + Creates a new backend-specific subclass of :class:`backend_bases.Timer`. + This is useful for getting periodic events through the backend's native + event loop. Implemented only for backends with GUIs. + + optional arguments: + + *interval* + Timer interval in milliseconds + *callbacks* + Sequence of (func, args, kwargs) where func(*args, **kwargs) will + be executed by the timer every *interval*. """ - return TimerGTK() + return TimerGTK(*args, **kwargs) def flush_events(self): gtk.gdk.threads_enter() Modified: trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py 2010-04-20 20:23:00 UTC (rev 8259) +++ trunk/matplotlib/lib/matplotlib/backends/backend_qt4.py 2010-04-20 22:37:01 UTC (rev 8260) @@ -97,8 +97,8 @@ upon timer events. This list can be manipulated directly, or the functions add_callback and remove_callback can be used. ''' - def __init__(self): - TimerBase.__init__(self) + def __init__(self, *args, **kwargs): + TimerBase.__init__(self, *args, **kwargs) # Create a new timer and connect the timeout() signal to the # _on_timer method. @@ -232,13 +232,21 @@ return key - def new_timer(self): + def new_timer(self, *args, **kwargs): """ - Creates a new backend-specific subclass of - :class:`backend_bases.TimerBase`. This is useful for getting periodic - events through the backend's native event loop. + Creates a new backend-specific subclass of :class:`backend_bases.Timer`. + This is useful for getting periodic events through the backend's native + event loop. Implemented only for backends with GUIs. + + optional arguments: + + *interval* + Timer interval in milliseconds + *callbacks* + Sequence of (func, args, kwargs) where func(*args, **kwargs) will + be executed by the timer every *interval*. """ - return TimerQT() + return TimerQT(*args, **kwargs) def flush_events(self): Qt.qApp.processEvents() Modified: trunk/matplotlib/lib/matplotlib/backends/backend_tkagg.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_tkagg.py 2010-04-20 20:23:00 UTC (rev 8259) +++ trunk/matplotlib/lib/matplotlib/backends/backend_tkagg.py 2010-04-20 22:37:01 UTC (rev 8260) @@ -108,8 +108,8 @@ upon timer events. This list can be manipulated directly, or the functions add_callback and remove_callback can be used. ''' - def __init__(self, parent): - TimerBase.__init__(self) + def __init__(self, parent, *args, **kwargs): + TimerBase.__init__(self, *args, **kwargs) self.parent = parent def _timer_start(self): @@ -126,7 +126,7 @@ # Tk after() is only a single shot, so we need to add code here to # reset the timer if we're not operating in single shot mode. if not self._single and len(self.callbacks) > 0: - self._timer = self.parent.after(self._interval, _self._on_timer) + self._timer = self.parent.after(self._interval, self._on_timer) else: self._timer = None @@ -358,13 +358,21 @@ key = self._get_key(event) FigureCanvasBase.key_release_event(self, key, guiEvent=event) - def new_timer(self): + def new_timer(self, *args, **kwargs): """ - Creates a new backend-specific subclass of - :class:`backend_bases.TimerBase`. This is useful for getting periodic - events through the backend's native event loop. + Creates a new backend-specific subclass of :class:`backend_bases.Timer`. + This is useful for getting periodic events through the backend's native + event loop. Implemented only for backends with GUIs. + + optional arguments: + + *interval* + Timer interval in milliseconds + *callbacks* + Sequence of (func, args, kwargs) where func(*args, **kwargs) will + be executed by the timer every *interval*. """ - return TimerTk() + return TimerTk(self._tkcanvas, *args, **kwargs) def flush_events(self): self._master.update() Modified: trunk/matplotlib/lib/matplotlib/backends/backend_wx.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_wx.py 2010-04-20 20:23:00 UTC (rev 8259) +++ trunk/matplotlib/lib/matplotlib/backends/backend_wx.py 2010-04-20 22:37:01 UTC (rev 8260) @@ -240,8 +240,8 @@ upon timer events. This list can be manipulated directly, or the functions add_callback and remove_callback can be used. ''' - def __init__(self, parent): - TimerBase.__init__(self) + def __init__(self, parent, *args, **kwargs): + TimerBase.__init__(self, *args, **kwargs) # Create a new timer and connect the timer event to our handler. # For WX, the events have to use a widget for binding. @@ -1022,13 +1022,21 @@ self._isDrawn = True self.gui_repaint(drawDC=drawDC) - def new_timer(self): + def new_timer(self, *args, **kwargs): """ - Creates a new backend-specific subclass of - :class:`backend_bases.TimerBase`. This is useful for getting periodic - events through the backend's native event loop. + Creates a new backend-specific subclass of :class:`backend_bases.Timer`. + This is useful for getting periodic events through the backend's native + event loop. Implemented only for backends with GUIs. + + optional arguments: + + *interval* + Timer interval in milliseconds + *callbacks* + Sequence of (func, args, kwargs) where func(*args, **kwargs) will + be executed by the timer every *interval*. """ - return TimerWx(self) + return TimerWx(self, *args, **kwargs) def flush_events(self): wx.Yield() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ _______________________________________________ Matplotlib-checkins mailing list Matplotlib-checkins@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins