]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/Timer.py
3 import wx
.lib
.scrolledpanel
as sp
5 #----------------------------------------------------------------------
9 This demo shows the various ways that wx.Timers can be used in your code. Just
10 select one of the buttons in the left column to start a timer in the indicated way,
11 and watch the log window below for messages printed when the timer event or callback
12 happens. Clicking the corresponding button on the right will stop that timer. Since
13 timers are not owned by any other wx object you should hold on to a reference to the
14 timer until you are completely finished with it. """
17 Binding an event handler to the wx.EVT_TIMER event is the
18 prefered way to use the wx.Timer class directly. It makes
19 handling timer events work just like normal window events. You
20 just need to specify the window that is to receive the event in
21 the wx.Timer constructor. If that window needs to be able to
22 receive events from more than one timer then you can optionally
23 specify an ID for the timer and the event binding.
28 wx.CallLater is a convenience class for wx.Timer. You just
29 specify the timeout in milliseconds and a callable object, along
30 with any args or keyword args you would like to be passed to your
31 callable, and wx.CallLater takes care of the rest. If you don't
32 need to get the return value of the callable or to restart the
33 timer then there is no need to hold a reference to this object.
38 If you derive a class from wx.Timer and give it a Notify method
39 then it will be called when the timer expires.
44 wx.PyTimer is the old way (a kludge that goes back all the way to
45 the first version of wxPython) to bind a timer directly to a
46 callable. You should migrate any code that uses this method to
47 use EVT_TIMER instead as this may be deprecated in the future.
51 class TestPanel(sp
.ScrolledPanel
):
52 def __init__(self
, parent
, log
):
54 sp
.ScrolledPanel
.__init
__(self
, parent
, -1)
56 outsideSizer
= wx
.BoxSizer(wx
.VERTICAL
)
58 text
= wx
.StaticText(self
, -1, "wx.Timer", style
=wx
.ALIGN_CENTRE
)
59 text
.SetFont(wx
.Font(24, wx
.SWISS
, wx
.NORMAL
, wx
.BOLD
, False))
60 text
.SetSize(text
.GetBestSize())
61 text
.SetForegroundColour(wx
.BLUE
)
62 outsideSizer
.Add(text
, 0, wx
.EXPAND|wx
.ALL
, 5)
63 outsideSizer
.Add(wx
.StaticText(self
, -1, header
), 0, wx
.ALIGN_CENTER|wx
.ALL
, 5)
64 outsideSizer
.Add(wx
.StaticLine(self
, -1), 0, wx
.EXPAND
)
65 outsideSizer
.Add((20,20))
68 t1b1
= wx
.Button(self
, -1, "EVT_TIMER")
69 t1b2
= wx
.Button(self
, -1, "stop timer")
70 t1st
= wx
.StaticText(self
, -1, doc1
)
72 self
.Bind(wx
.EVT_BUTTON
, self
.OnTest1Start
, t1b1
)
73 self
.Bind(wx
.EVT_BUTTON
, self
.OnTest1Stop
, t1b2
)
75 # Bind all EVT_TIMER events to self.OnTest1Timer
76 self
.Bind(wx
.EVT_TIMER
, self
.OnTest1Timer
)
79 t2b1
= wx
.Button(self
, -1, "wx.CallLater")
80 t2b2
= wx
.Button(self
, -1, "stop timer")
81 t2st
= wx
.StaticText(self
, -1, doc2
)
83 self
.Bind(wx
.EVT_BUTTON
, self
.OnTest2Start
, t2b1
)
84 self
.Bind(wx
.EVT_BUTTON
, self
.OnTest2Stop
, t2b2
)
86 t3b1
= wx
.Button(self
, -1, "self.Notify")
87 t3b2
= wx
.Button(self
, -1, "stop timer")
88 t3st
= wx
.StaticText(self
, -1, doc3
)
90 self
.Bind(wx
.EVT_BUTTON
, self
.OnTest3Start
, t3b1
)
91 self
.Bind(wx
.EVT_BUTTON
, self
.OnTest3Stop
, t3b2
)
93 t4b1
= wx
.Button(self
, -1, "wx.PyTimer")
94 t4b2
= wx
.Button(self
, -1, "stop timer")
95 t4st
= wx
.StaticText(self
, -1, doc4
)
97 self
.Bind(wx
.EVT_BUTTON
, self
.OnTest4Start
, t4b1
)
98 self
.Bind(wx
.EVT_BUTTON
, self
.OnTest4Stop
, t4b2
)
106 fgs
= wx
.FlexGridSizer(cols
=3, hgap
=10, vgap
=10)
123 outsideSizer
.Add(fgs
, 0, wx
.ALIGN_CENTER|wx
.ALL
, 10)
124 self
.SetSizer(outsideSizer
)
125 self
.SetupScrolling()
128 # Test 1 shows how to use a timer to generate EVT_TIMER
129 # events, by passing self to the wx.Timer constructor. The
130 # event is bound above to the OnTest1Timer method.
132 def OnTest1Start(self
, evt
):
133 self
.t1
= wx
.Timer(self
)
135 self
.log
.write("EVT_TIMER timer started\n")
138 def OnTest1Stop(self
, evt
):
140 self
.log
.write("EVT_TIMER timer stoped\n")
144 def OnTest1Timer(self
, evt
):
145 self
.log
.write("got EVT_TIMER event\n")
149 # Test 2 shows how to use the wx.CallLater class.
151 def OnTest2Start(self
, evt
):
152 # Call OnTest2Timer one second in the future, passing some
153 # optional arbitrary args. There is no need to hold a
154 # reference to this one, unless we want to manipulate or query
155 # it later like we do in the two methods below
156 self
.t2
= wx
.CallLater(1000, self
.OnTest2Timer
,
157 'a', 'b', 'c', one
=1, two
=2)
158 self
.log
.write("CallLater scheduled\n")
161 def OnTest2Stop(self
, evt
):
163 self
.log
.write("CallLater stopped, last return value was: %s\n" %
164 repr(self
.t2
.GetResult()))
168 def OnTest2Timer(self
, *args
, **kw
):
169 self
.log
.write("CallLater called with args=%s, kwargs=%s\n" % (args
, kw
))
171 # Normally a FutureCall is one-shot, but we can make it
172 # recurring just by calling Restart. We can even use a
173 # different timeout or pass differnt args this time.
174 self
.t2
.Restart(1500, "restarted")
176 # The return value of this function is saved and can be
177 # retrived later. See OnTest2Stop above.
178 return "This is my return value"
182 # Test 3 shows how to use a class derived from wx.Timer. See
183 # also the NotifyTimer class below.
185 def OnTest3Start(self
, evt
):
186 self
.t3
= NotifyTimer(self
.log
)
188 self
.log
.write("NotifyTimer timer started\n")
191 def OnTest3Stop(self
, evt
):
193 self
.log
.write("NotifyTimer timer stoped\n")
199 # Test 4 shows the old way (a kludge that goes back all the
200 # way to the first version of wxPython) to bind a timer
201 # directly to a callable. You should migrate any code that
202 # uses this method to use EVT_TIMER instead as this may be
203 # deprecated in the future.
204 def OnTest4Start(self
, evt
):
205 self
.t4
= wx
.PyTimer(self
.OnTest4Timer
)
207 self
.log
.write("wx.PyTimer timer started\n")
210 def OnTest4Stop(self
, evt
):
212 self
.log
.write("wx.PyTimer timer stoped\n")
216 def OnTest4Timer(self
):
217 self
.log
.write("got wx.PyTimer event\n")
221 #----------------------------------------------------------------------
224 # When deriving from wx.Timer you must provide a Notify method
225 # that will be called when the timer expires.
226 class NotifyTimer(wx
.Timer
):
227 def __init__(self
, log
):
228 wx
.Timer
.__init
__(self
)
232 self
.log
.write("got NotifyTimer event\n")
236 #----------------------------------------------------------------------
238 def runTest(frame
, nb
, log
):
239 win
= TestPanel(nb
, log
)
243 #----------------------------------------------------------------------
245 overview
= """<html><body>
246 <h2><center>wx.Timer</center></h2>
248 The wx.Timer class allows you to execute code at specified intervals
249 from within the wxPython event loop. Timers can be one-shot or
250 repeating. This demo shows the principle method of using a timer
251 (with events) as well as the convenient wx.FutureCall class. Also
252 there are two other usage patterns shown here that have been preserved
253 for backwards compatibility.
260 if __name__
== '__main__':
263 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])