adamsmith
adamsmith

Reputation: 5999

wxpython Timer not working

As reading "wxPython in Action" I tried the code in the book, a simplified piece, as follows:

import wx 
import time

class Frame(wx.Frame):  
    def __init__(self, *args, **kwargs):  
        wx.Frame.__init__(self, *args, **kwargs)  
        timer = wx.Timer(self,-1)  
        self.Bind(wx.EVT_TIMER, self.OnTimer,timer)   
        timer.Start(1000, True)  
        self.Show()  
    def OnTimer(self, evt):  
        print time.time(), evt   

app = wx.App(False) 
frm = Frame(None, -1)  
app.MainLoop()

When I run this, python gives no error, but it does not print anything either.
weird, anybody knows why?

Upvotes: 1

Views: 954

Answers (2)

Mike Driscoll
Mike Driscoll

Reputation: 33071

The reason that doesn't work is because the timer goes out of scope at the end of the init method and doesn't really have a chance to run. As GP89 already pointed out, you need to just change it to be "self.timer" and it should work. I also have a tutorial you can check out.

Upvotes: 4

GP89
GP89

Reputation: 6730

Change timer to self.timer and it will work.

I'd be interested to know the reason why it doesn't work as a local variable

I'd guess at something to do with garbage collection though

Upvotes: 1

Related Questions