1
"""Example of using a pyface Timer."""
4
from pyface.timer.api import Timer
5
from pyface.api import GUI, ApplicationWindow
6
from pyface.action.api import Action, MenuManager, MenuBarManager
7
from traits.api import Any, Int
9
class MainWindow(ApplicationWindow):
10
""" The main application window. """
15
# Count each time the timer task executes.
18
def __init__(self, **traits):
19
""" Creates a new application window. """
21
# Base class constructor.
22
super(MainWindow, self).__init__(**traits)
25
self.menu_bar_manager = MenuBarManager(
27
Action(name='Start Timer', on_perform=self._start_timer),
28
Action(name='Stop Timer', on_perform=self._stop_timer),
29
Action(name='E&xit', on_perform=self.close),
36
def _start_timer(self):
37
"""Called when the user selects "Start Timer" from the menu."""
39
if self.my_timer is None:
40
# First call, so create a Timer. It starts automatically.
41
self.my_timer = Timer(500, self._timer_task)
45
def _stop_timer(self):
46
"""Called when the user selecte "Stop Timer" from the menu."""
48
if self.my_timer is not None:
52
def _timer_task(self):
53
"""The method run periodically by the timer."""
56
print "counter = %d" % self.counter
59
if __name__ == "__main__":
62
# Create and open the main window.
66
# Start the GUI event loop!
67
gui.start_event_loop()