~ubuntu-branches/ubuntu/trusty/python-pyface/trusty

« back to all changes in this revision

Viewing changes to examples/timer.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 10:57:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110709105754-k4fw9am00s9vj5yf
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Example of using a pyface Timer."""
 
2
 
 
3
 
 
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
 
8
 
 
9
class MainWindow(ApplicationWindow):
 
10
    """ The main application window. """
 
11
 
 
12
    # The pyface Timer.
 
13
    my_timer = Any()
 
14
 
 
15
    # Count each time the timer task executes.
 
16
    counter = Int
 
17
 
 
18
    def __init__(self, **traits):
 
19
        """ Creates a new application window. """
 
20
 
 
21
        # Base class constructor.
 
22
        super(MainWindow, self).__init__(**traits)
 
23
 
 
24
        # Add a menu bar.
 
25
        self.menu_bar_manager = MenuBarManager(
 
26
            MenuManager(
 
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),
 
30
                name = '&File',
 
31
            )
 
32
        )
 
33
 
 
34
        return
 
35
 
 
36
    def _start_timer(self):
 
37
        """Called when the user selects "Start Timer" from the menu."""
 
38
 
 
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)
 
42
        else:
 
43
            self.my_timer.Start()
 
44
 
 
45
    def _stop_timer(self):
 
46
        """Called when the user selecte "Stop Timer" from the menu."""
 
47
 
 
48
        if self.my_timer is not None:
 
49
            self.my_timer.Stop()
 
50
 
 
51
 
 
52
    def _timer_task(self):
 
53
        """The method run periodically by the timer."""
 
54
 
 
55
        self.counter += 1
 
56
        print "counter = %d" % self.counter
 
57
 
 
58
 
 
59
if __name__ == "__main__":
 
60
    gui = GUI()
 
61
 
 
62
    # Create and open the main window.
 
63
    window = MainWindow()
 
64
    window.open()
 
65
 
 
66
    # Start the GUI event loop!
 
67
    gui.start_event_loop()
 
68