Can't make wx.Timer work correctly.

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

···

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

The parentheses :slight_smile: . This line:

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

Should be:

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

Andrea.

···

On Wednesday, 14 September 2016, Steve Ihnen <wrote:

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.

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

···

Andrea Gavana wrote:

The parentheses :slight_smile: . This line:

self.ClockTimer.Bind(wx.EVT_ TIMER,
self.OnClockTick())

Should be:

self.ClockTimer.Bind(wx.EVT_ TIMER,
self.OnClockTick)

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com