Well, a straight translation from C++ to Python would be
something like this:
01: class MyFrame(wxFrame):
02: def __init__(self, *args):
03: wxFrame.__init__(self, *args)
04: self.m_timer = wxTimer(self, TIMER_ID)
05: EVT_TIMER(self, TIMER_ID, self.OnTimer)
06: self.m_timer.Start(1000)
07: def OnTimer(self, event):
08: # do whatever you want to do every second here
09: print "Something happened"
Here's the stucture from the manual:
class MyFrame : public wxFrame
Line 01.
{
public:
...
void OnTimer(wxTimerEvent& event);
Not declared in python, but defined in line 07.
private:
wxTimer m_timer;
Not declared in python, but defined in __init__ line 04.
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
END_EVENT_TABLE()
Put this in MyFrame.__init__ in Python. Line 05.
MyFrame::MyFrame()
A method with the same name as the class in C++ is a constructor,
more or less the same as __init__ in Python.
: m_timer(this, TIMER_ID)
Line 04.
{
m_timer.Start(1000); // 1 second interval
Line 06.
}
void MyFrame::OnTimer(wxTimerEvent& event)
{
// do whatever you want to do every second here
}
Lines 07-08.
===
Again,.... i still confuse how to translate it to python. can anyone
translate this, and give me a realistic example (i can try to run it)?
So, you see, you need to fill in the explicit 'self' that are implied
in C++, otherwise you mainly strip away all extra noise C++ carries.
Also event tables go inside __init__.
Here it is as a functional app:
···
At 13:25 2001-10-29 +0700, you wrote:
#####################################
from wxPython.wx import *
TIMER_ID = 6789
class MyFrame(wxFrame):
def __init__(self, *args):
wxFrame.__init__(self, *args)
self.m_timer = wxTimer(self, TIMER_ID)
EVT_TIMER(self, TIMER_ID, self.OnTimer)
self.m_timer.Start(1000)
def OnTimer(self, event):
# do whatever you want to do every second here
print "Something happened"
class MyApp(wxApp):
def OnInit(self):
self.frame = MyFrame(NULL, -1, "Test of wxTimer")
self.frame.Show(true)
self.SetTopWindow(self.frame)
return true
def main():
app = MyApp(0)
app.MainLoop()
if __name__ == '__main__':
main()
#####################################
There is also a good example in the Demo, under Miscellaneous->wxTimer.
--
Magnus Lyckå, Thinkware AB
Älvans väg 99, SE-907 50 UMEÅ
tel 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se