From 660658398148d82d492a5d1a944bc4058a082267 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Tue, 2 Mar 2004 23:44:42 +0000 Subject: [PATCH] Fixed wx.Timer to not need to hold an extra reference to itself, so it can be cleaned up properly when the Python reference to it is released. Also fixed wx.FutureCall so that its timer can be restarted. Souped up the Timer demo to show and explain the various ways to use a wx.Timer. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@26033 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/demo/Timer.py | 304 ++++++++++++++++++++++++++++++--------- wxPython/src/_core_ex.py | 9 +- wxPython/src/_timer.i | 34 ++++- 3 files changed, 269 insertions(+), 78 deletions(-) diff --git a/wxPython/demo/Timer.py b/wxPython/demo/Timer.py index 86892c7f88..3c88d92a39 100644 --- a/wxPython/demo/Timer.py +++ b/wxPython/demo/Timer.py @@ -1,101 +1,267 @@ -# -# 1/11/2004 - Jeff Grimmett (grimmtooth@softhome.net) -# -# o It appears that wx.Timer has an issue where if you use -# -# self.timer = wx.Timer(self, -1) -# -# to create it, then -# -# self.timer.GetId() -# -# doesn't seem to return anything meaningful. In the demo, doing this -# results in only one of the two handlers being called for both timers. -# This means that -# -# self.Bind(wx.EVT_TIMER, self.onTimer, self.timer) -# -# doesn't work right. However, using -# -# self.timer = wx.Timer(self, wx.NewId()) -# -# makes it work OK. I believe this is a bug, but wiser heads than mine -# should determine this. -# - import time import wx +import wx.lib.scrolledpanel as sp + +#---------------------------------------------------------------------- + + +header = """\ +This demo shows the various ways that wx.Timers can be used in your code. Just +select one of the buttons in the left column to start a timer in the indicated way, +and watch the log window below for messages printed when the timer event or callback +happens. Clicking the corresponding button on the right will stop that timer. Since +timers are not owned by any other wx object you should hold on to a reference to the +timer until you are completely finished with it. """ + +doc1 = """\ +Binding an event handler to the wx.EVT_TIMER event is the +prefered way to use the wx.Timer class directly. It makes +handling timer events work just like normal window events. You +just need to specify the window that is to receive the event in +the wx.Timer constructor. If that window needs to be able to +receive events from more than one timer then you can optionally +specify an ID for the timer and the event binding. +""" + + +doc2 = """\ +wx.FutureCall is a convenience class for wx.Timer. You just +specify the timeout in milliseconds and a callable object, along +with any ard or keyword arg you woudl like to be passed to your +callable, and wx.FutureCall takes care of the rest. If you don't +need to get the return value of the callable or to restart the +timer then there is no need to hold a reference to this object. +""" + + +doc3 = """\ +If you derive a class from wx.Timer and give it a Notify method +then it will be called when the timer expires. +""" -#--------------------------------------------------------------------------- -## For your convenience; an example of creating your own timer class. -## -## class TestTimer(wx.Timer): -## def __init__(self, log = None): -## wx.Timer.__init__(self) -## self.log = log -## def Notify(self): -## wx.Bell() -## if self.log: -## self.log.WriteText('beep!\n') +doc4 = """\ +wx.PyTimer is the old way (a kludge that goes back all the way to +the first version of wxPython) to bind a timer directly to a +callable. You should migrate any code that uses this method to +use EVT_TIMER instead as this may be deprecated in the future. +""" -#--------------------------------------------------------------------------- -class TestTimerWin(wx.Panel): +class TestPanel(sp.ScrolledPanel): def __init__(self, parent, log): - wx.Panel.__init__(self, parent, -1) self.log = log + sp.ScrolledPanel.__init__(self, parent, -1) + + outsideSizer = wx.BoxSizer(wx.VERTICAL) + + text = wx.StaticText(self, -1, "wx.Timer", style=wx.ALIGN_CENTRE) + text.SetFont(wx.Font(24, wx.SWISS, wx.NORMAL, wx.BOLD, False)) + text.SetSize(text.GetBestSize()) + text.SetForegroundColour(wx.BLUE) + outsideSizer.Add(text, 0, wx.EXPAND|wx.ALL, 5) + outsideSizer.Add(wx.StaticText(self, -1, header), 0, wx.ALIGN_CENTER|wx.ALL, 5) + outsideSizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND) + outsideSizer.Add((20,20)) + + + t1b1 = wx.Button(self, -1, "EVT_TIMER") + t1b2 = wx.Button(self, -1, "stop timer") + t1st = wx.StaticText(self, -1, doc1) + t1b2.Disable() + self.Bind(wx.EVT_BUTTON, self.OnTest1Start, t1b1) + self.Bind(wx.EVT_BUTTON, self.OnTest1Stop, t1b2) + + # Bind all EVT_TIMER events to self.OnTest1Timer + self.Bind(wx.EVT_TIMER, self.OnTest1Timer) + + + t2b1 = wx.Button(self, -1, "wx.FutureCall") + t2b2 = wx.Button(self, -1, "stop timer") + t2st = wx.StaticText(self, -1, doc2) + t2b2.Disable() + self.Bind(wx.EVT_BUTTON, self.OnTest2Start, t2b1) + self.Bind(wx.EVT_BUTTON, self.OnTest2Stop, t2b2) + + t3b1 = wx.Button(self, -1, "self.Notify") + t3b2 = wx.Button(self, -1, "stop timer") + t3st = wx.StaticText(self, -1, doc3) + t3b2.Disable() + self.Bind(wx.EVT_BUTTON, self.OnTest3Start, t3b1) + self.Bind(wx.EVT_BUTTON, self.OnTest3Stop, t3b2) + + t4b1 = wx.Button(self, -1, "wx.PyTimer") + t4b2 = wx.Button(self, -1, "stop timer") + t4st = wx.StaticText(self, -1, doc4) + t4b2.Disable() + self.Bind(wx.EVT_BUTTON, self.OnTest4Start, t4b1) + self.Bind(wx.EVT_BUTTON, self.OnTest4Stop, t4b2) + + + self.t1b2 = t1b2 + self.t2b2 = t2b2 + self.t3b2 = t3b2 + self.t4b2 = t4b2 + + fgs = wx.FlexGridSizer(cols=3, hgap=10, vgap=10) + fgs.Add(t1b1) + fgs.Add(t1b2) + fgs.Add(t1st) - wx.StaticText(self, -1, "This is a timer example", (15, 30)) - startBtn = wx.Button(self, -1, ' Start ', (15, 75), wx.DefaultSize) - stopBtn = wx.Button(self, -1, ' Stop ', (115, 75), wx.DefaultSize) + fgs.Add(t2b1) + fgs.Add(t2b2) + fgs.Add(t2st) - self.timer = wx.Timer(self, wx.NewId()) - self.timer2 = wx.Timer(self, wx.NewId()) + fgs.Add(t3b1) + fgs.Add(t3b2) + fgs.Add(t3st) - self.Bind(wx.EVT_BUTTON, self.OnStart, startBtn) - self.Bind(wx.EVT_BUTTON, self.OnStop, stopBtn) - self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer) - self.Bind(wx.EVT_TIMER, self.OnTimer2, self.timer2) + fgs.Add(t4b1) + fgs.Add(t4b2) + fgs.Add(t4st) + + outsideSizer.Add(fgs, 0, wx.ALIGN_CENTER|wx.ALL, 10) + self.SetSizer(outsideSizer) + self.SetupScrolling() + - def OnStart(self, event): - self.timer.Start(1000) - self.timer2.Start(1500) + # Test 1 shows how to use a timer to generate EVT_TIMER + # events, by passing self to the wx.Timer constructor. The + # event is bound above to the OnTest1Timer method. + + def OnTest1Start(self, evt): + self.t1 = wx.Timer(self) + self.t1.Start(1000) + self.log.write("EVT_TIMER timer started\n") + self.t1b2.Enable() - def OnStop(self, event): - self.timer.Stop() - self.timer2.Stop() + def OnTest1Stop(self, evt): + self.t1.Stop() + self.log.write("EVT_TIMER timer stoped\n") + del self.t1 + self.t1b2.Disable() - def OnTimer(self, event): - wx.Bell() - if self.log: - self.log.WriteText('beep!\n') + def OnTest1Timer(self, evt): + self.log.write("got EVT_TIMER event\n") + - def OnTimer2(self, event): - wx.Bell() - if self.log: - self.log.WriteText('beep 2!\n') -#--------------------------------------------------------------------------- + # Test 2 shows how to use the wx.FutureCall class. + + def OnTest2Start(self, evt): + # Call OnTest2Timer one second in the future, passing some + # optional arbitrary args. There is no need to hold a + # reference to this one, unless we want to manipulate or query + # it later like we do in the two methods below + self.t2 = wx.FutureCall(1000, self.OnTest2Timer, + 'a', 'b', 'c', one=1, two=2) + self.log.write("FutureCall scheduled\n") + self.t2b2.Enable() + + def OnTest2Stop(self, evt): + self.t2.Stop() + self.log.write("FutureCall stopped, last return value was: %s\n" % + repr(self.t2.GetResult())) + del self.t2 + self.t2b2.Disable() + + def OnTest2Timer(self, *args, **kw): + self.log.write("FutureCall called with args=%s, kwargs=%s\n" % (args, kw)) + + # Normally a FutureCall is one-shot, but we can make it + # recurring just by calling Restart. We can even use a + # different timeout or pass differnt args this time. + self.t2.Restart(1500, "restarted") + + # The return value of this function is saved and can be + # retrived later. See OnTest2Stop above. + return "This is my return value" + + + + # Test 3 shows how to use a class derived from wx.Timer. See + # also the NotifyTimer class below. + + def OnTest3Start(self, evt): + self.t3 = NotifyTimer(self.log) + self.t3.Start(1000) + self.log.write("NotifyTimer timer started\n") + self.t3b2.Enable() + + def OnTest3Stop(self, evt): + self.t3.Stop() + self.log.write("NotifyTimer timer stoped\n") + del self.t3 + self.t3b2.Disable() + + + + # Test 4 shows the old way (a kludge that goes back all the + # way to the first version of wxPython) to bind a timer + # directly to a callable. You should migrate any code that + # uses this method to use EVT_TIMER instead as this may be + # deprecated in the future. + def OnTest4Start(self, evt): + self.t4 = wx.PyTimer(self.OnTest4Timer) + self.t4.Start(1000) + self.log.write("wx.PyTimer timer started\n") + self.t4b2.Enable() + + def OnTest4Stop(self, evt): + self.t4.Stop() + self.log.write("wx.PyTimer timer stoped\n") + del self.t4 + self.t4b2.Disable() + + def OnTest4Timer(self): + self.log.write("got wx.PyTimer event\n") + + + +#---------------------------------------------------------------------- + + +# When deriving from wx.Timer you must provide a Notify method +# that will be called when the timer expires. +class NotifyTimer(wx.Timer): + def __init__(self, log): + wx.Timer.__init__(self) + self.log = log + + def Notify(self): + log.write("got NotifyTimer event\n") + + + +#---------------------------------------------------------------------- def runTest(frame, nb, log): - win = TestTimerWin(nb, log) + win = TestPanel(nb, log) return win -#--------------------------------------------------------------------------- +#---------------------------------------------------------------------- +overview = """ +

wx.Timer

-overview = """\ -The wx.Timer class allows you to execute code at specified intervals from -within the wxPython event loop. Timers can be one-shot or repeating. +The wx.Timer class allows you to execute code at specified intervals +from within the wxPython event loop. Timers can be one-shot or +repeating. This demo shows the principle method of using a timer +(with events) as well as the convenient wx.FutureCall class. Also +there are two other usage patterns shown here that have been preserved +for backwards compatibility. + """ - if __name__ == '__main__': import sys,os import run run.main(['', os.path.basename(sys.argv[0])]) + + + + diff --git a/wxPython/src/_core_ex.py b/wxPython/src/_core_ex.py index 52edf3c955..fdfe05f5b6 100644 --- a/wxPython/src/_core_ex.py +++ b/wxPython/src/_core_ex.py @@ -117,7 +117,7 @@ def CallAfter(callable, *args, **kw): class FutureCall: """ - A convenience class for wxTimer, that calls the given callable + A convenience class for wx.Timer, that calls the given callable object once after the given amount of milliseconds, passing any positional or keyword args. The return value of the callable is availbale after it has been run with the GetResult method. @@ -134,6 +134,7 @@ class FutureCall: self.callable = callable self.SetArgs(*args, **kwargs) self.runCount = 0 + self.running = False self.hasRun = False self.result = None self.timer = None @@ -155,6 +156,7 @@ class FutureCall: self.Stop() self.timer = wx.PyTimer(self.Notify) self.timer.Start(self.millis, wx.TIMER_ONE_SHOT) + self.running = True Restart = Start @@ -201,9 +203,12 @@ class FutureCall: """ if self.callable and getattr(self.callable, 'im_self', True): self.runCount += 1 + self.running = False self.result = self.callable(*self.args, **self.kwargs) self.hasRun = True - wx.CallAfter(self.Stop) + if not self.running: + # if it wasn't restarted, then cleanup + wx.CallAfter(self.Stop) #---------------------------------------------------------------------------- diff --git a/wxPython/src/_timer.i b/wxPython/src/_timer.i index a82aa5a626..5471ca7b26 100644 --- a/wxPython/src/_timer.i +++ b/wxPython/src/_timer.i @@ -26,13 +26,27 @@ enum { // Timer event type %constant wxEventType wxEVT_TIMER; - + //--------------------------------------------------------------------------- %{ -IMP_PYCALLBACK__(wxPyTimer, wxTimer, Notify); +//IMP_PYCALLBACK__(wxPyTimer, wxTimer, Notify); + +void wxPyTimer::Notify() { + bool found; + wxPyBeginBlockThreads(); + if ((found = wxPyCBH_findCallback(m_myInst, "Notify"))) + wxPyCBH_callCallback(m_myInst, Py_BuildValue("()")); + wxPyEndBlockThreads(); + if (! found) + wxTimer::Notify(); +} +void wxPyTimer::base_Notify() { + wxTimer::Notify(); +} + %} @@ -40,7 +54,13 @@ IMP_PYCALLBACK__(wxPyTimer, wxTimer, Notify); %name(Timer) class wxPyTimer : public wxEvtHandler { public: - %pythonAppend wxPyTimer "self._setCallbackInfo(self, Timer)" + %pythonAppend wxPyTimer "self._setCallbackInfo(self, Timer, 0)" + +// %pythonAppend wxTimer +// "if hasattr(self, 'Notify'): +// print 'bound EVT_TIMER to self.Notify' +// self.Bind(EVT_TIMER, self.Notify) +// self.SetOwner(self)"; // if you don't call SetOwner() or provide an owner in the contstructor // then you must override Notify() inorder to receive the timer @@ -49,7 +69,7 @@ public: wxPyTimer(wxEvtHandler *owner=NULL, int id = -1); virtual ~wxPyTimer(); - void _setCallbackInfo(PyObject* self, PyObject* _class); + void _setCallbackInfo(PyObject* self, PyObject* _class, int incref=1); // Set the owner instance that will receive the EVT_TIMER events using the // given id. @@ -81,11 +101,11 @@ public: // return the timer ID int GetId() const; - + }; -%pythoncode { +%pythoncode { %# For backwards compatibility with 2.4 class PyTimer(Timer): def __init__(self, notify): @@ -98,7 +118,7 @@ class PyTimer(Timer): EVT_TIMER = wx.PyEventBinder( wxEVT_TIMER, 1 ) - + }; -- 2.47.2