Can't make wx.Timer work correctly.

閲覧: 75 回
最初の未読メッセージにスキップ

Steve Ihnen

未読、
2016/09/13 22:45:412016/09/13
To: wxPython-users
I am trying a very simple task.

I want to have a timer, with interval of 1 second, which throws an event at that interval.  When the event is thrown it should run a method, then return.

This is my code, including the last few lines of my init:


        self.ClockTimer = wx.Timer(self,-1)
        self.ClockTimer.Start(1000,False)
        self.ClockTimer.Bind(wx.EVT_TIMER, self.OnClockTick())
        #self.Bind(wx.EVT_TIMER, self.OnClockTick(),self.ClockTimer)
       
    def OnClockTick(self):
        self.txtLocalTime.SetLabel(time.strftime("%I:%M:%S %p", time.localtime()))
        print(time.strftime("%I:%M:%S %p", time.localtime()))
        return
      

I was expecting the print statement to execute once every second.   Instead the print statement prints once, then not again.  It behaves very much as if oneShot were True, rather than False as it explicitly is.

Any suggestions?

Steve
       

Andrea Gavana

未読、
2016/09/14 0:30:272016/09/14
To: wxpytho...@googlegroups.com
The parentheses :-) . This line:

 self.ClockTimer.Bind(wx.EVT_TIMER, self.OnClockTick())

Should be:

self.ClockTimer.Bind(wx.EVT_TIMER, self.OnClockTick)

Andrea.


--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Tim Roberts

未読、
2016/09/14 13:49:132016/09/14
To: wxpytho...@googlegroups.com
Andrea Gavana wrote:

The parentheses :-) . This line:

 self.ClockTimer.Bind(wx.EVT_TIMER, self.OnClockTick())

Should be:

self.ClockTimer.Bind(wx.EVT_TIMER, self.OnClockTick)

As a side note, this is a VERY easy mistake to make.  It's one that Python can't catch, and it is difficult to detect, because as you noted, the function did seem to get called once.  For beginners, it's worth taking a minute to work out exactly why this fails, so that you get in the habit of triple-checking every call to Bind.

As a hint, the parens mean that OnClockTick was called before Bind was called, when Python was preparing the parameters.  OnClockTick returns None, so what Bind actually saw was (wx.EVT_TIMER, None);
-- 
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.
全員に返信
投稿者に返信
転送
新着メール 0 件