[wxPython] using wxStopWatch

i’d like to ask about using wxStopWatch. I did

···

===

from wxPython.wx import *

class Frame(wxFrame):

mySW = wxStopWatch()

===

I got “NameError: There is no variable named wxStopWatch’”

I’d like to show the running time on the panel, and like to do something every 10 seconds. Can you explain to me the structure? The manual got me confuse, i don’t familiar with C stuff. please help me.

thanks,

Adinda

(learning wxPython)

I got "NameError: There is no variable named wxStopWatch'"

It's not in there yet. I'll add it.

I'd like to show the running time on the panel, and like to
do something every 10 seconds.

You actually want to use a wxTimer. It will be able to send you an event
every 10 seconds. With a stopwatch you would have to constantly check it to
see if the 10 seconds have elapsed.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

Here's the stucture from the manual:

···

===
class MyFrame : public wxFrame
{
public:
    ...
    void OnTimer(wxTimerEvent& event);

private:
    wxTimer m_timer;
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
END_EVENT_TABLE()

MyFrame::MyFrame()
       : m_timer(this, TIMER_ID)
{
    m_timer.Start(1000); // 1 second interval
}

void MyFrame::OnTimer(wxTimerEvent& event)
{
    // do whatever you want to do every second here
}

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)?

thank you,

Adinda

----- Original Message -----
From: "Robin Dunn" <robin@alldunn.com>
To: <wxpython-users@lists.wxwindows.org>
Sent: Friday, October 26, 2001 10:57 PM
Subject: Re: [wxPython] using wxStopWatch

>
> I got "NameError: There is no variable named wxStopWatch'"

It's not in there yet. I'll add it.

>
> I'd like to show the running time on the panel, and like to
> do something every 10 seconds.

You actually want to use a wxTimer. It will be able to send you an event
every 10 seconds. With a stopwatch you would have to constantly check it

to

see if the 10 seconds have elapsed.

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

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

Thanks for your explanation Magnus, i'm wondering what's this for?

if __name__ == '__main__':
     main()

···

#####################################

There is also a good example in the Demo, under Miscellaneous->wxTimer.

I'd like to put realtime stopwatch. Like i said before, i get error when i
write "self.stopw = wxStopWatch()",
though it's exist in the manual 2.3.1. I still confuse how i'm gonna do
that.

--

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

once again thanks,

Adinda Praditya

Thanks for your explanation Magnus, i'm wondering what's this for?

if __name__ == '__main__':
     main()

It calls main() only if the name of the module is "__main__", which it will
be if it is executed on the python command line rather than imported.

#####################################

> There is also a good example in the Demo, under Miscellaneous->wxTimer.

I'd like to put realtime stopwatch. Like i said before, i get error when i
write "self.stopw = wxStopWatch()",
though it's exist in the manual 2.3.1. I still confuse how i'm gonna do
that.

Either use a timer instead or wait for the next 2.3.2 beta.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!