wx.Timer question

Hello,

I use a wx.Timer and I had some problems with it. The timer did not work when I didn’t assign the timer object to a member variable. Here is my sample code:

import wx

class TestFrame(wx.Frame):

def __init__(self):

    wx.Frame.__init__(self, None, -1, "Test")

    panel = wx.Panel(self, -1)

    sizer = wx.BoxSizer(wx.VERTICAL)

    panel.SetSizer(sizer)

    

    timer1 = wx.Timer(self, id=1)

    timer1.Start(10, oneShot=True)

    

    timer2 = wx.Timer(self, id=2)

    timer2.Start(10, oneShot=True)

    self.Bind(wx.EVT_TIMER, self._OnTimer, timer1)

    self.Bind(wx.EVT_TIMER, self._OnTimer2, timer2)

    

    self.timer = timer1 # <=== without this line the timer does not work

    self.timer2 = timer2 # <=== without this line the timer does not work

def _OnTimer(self, event):

    print "_OnTimer", event.GetId()

def _OnTimer2(self, event):

    print "_OnTimer2", event.GetId()

app = wx.PySimpleApp()

TestFrame().Show()

app.MainLoop()

Questions:

Why is this assignment necessary ? Is the timer object garbage collected when it is not assigned to the member variable ?

wx: 2.8.12.1

Windows Vista

Erwin

Hi Erwin,

···

On Tuesday, September 17, 2013 3:27:20 PM UTC-5, ErwinP wrote:

Hello,

I use a wx.Timer and I had some problems with it. The timer did not work when I didn’t assign the timer object to a member variable. Here is my sample code:

import wx

class TestFrame(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, None, -1, "Test")
    panel = wx.Panel(self, -1)
    sizer = wx.BoxSizer(wx.VERTICAL)
    panel.SetSizer(sizer)
    timer1 = wx.Timer(self, id=1)
    timer1.Start(10, oneShot=True)
    timer2 = wx.Timer(self, id=2)
    timer2.Start(10, oneShot=True)
    self.Bind(wx.EVT_TIMER, self._OnTimer, timer1)
    self.Bind(wx.EVT_TIMER, self._OnTimer2, timer2)
    self.timer = timer1 # <=== without this line the timer does not work
    self.timer2 = timer2 # <=== without this line the timer does not work
def _OnTimer(self, event):
    print "_OnTimer", event.GetId()
def _OnTimer2(self, event):
    print "_OnTimer2", event.GetId()

app = wx.PySimpleApp()

TestFrame().Show()

app.MainLoop()

Questions:

Why is this assignment necessary ? Is the timer object garbage collected when it is not assigned to the member variable ?

wx: 2.8.12.1

Windows Vista

Erwin

Yes. Once the init method finishes, anything that isn’t a member variable is garbage collected.

  • Mike

I really just wanted to comment the use of constants for ID's, but I don't know
if it's related to the problem.

         timer1 = wx.Timer(self, id=1)

instead of id=1, just use id=-1 (or better, id=wx.NewId())and let the system assign one. Otherwise you may be
choosing an id already in use.

But for your question: You may be right about the garbage collector causing
some kind of deconstruction when the local variables are out of scope. You would
think that the binding would have a reference to the timer object to prevent that.

···

On 9/17/2013 4:27 PM, ErwinP wrote:

Hello,

I use a wx.Timer and I had some problems with it. The timer did not work when I didn't assign the timer object to a member variable. Here is my sample code:

import wx

class TestFrame(wx.Frame):
    def __init__(self):
wx.Frame.__init__(self, None, -1, "Test")
        panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(sizer)

        timer1 = wx.Timer(self, id=1)
timer1.Start(10, oneShot=True)
        timer2 = wx.Timer(self, id=2)
timer2.Start(10, oneShot=True)

self.Bind(wx.EVT_TIMER, self._OnTimer, timer1)
self.Bind(wx.EVT_TIMER, self._OnTimer2, timer2)
        self.timer = timer1 # <=== without this line the timer does not work
        self.timer2 = timer2# <=== without this line the timer does not work

    def _OnTimer(self, event):
        print "_OnTimer", event.GetId()

    def _OnTimer2(self, event):
        print "_OnTimer2", event.GetId()

app = wx.PySimpleApp()
TestFrame().Show()
app.MainLoop()

Questions:
Why is this assignment necessary ? Is the timer object garbage collected when it is not assigned to the member variable ?

wx: 2.8.12.1
Windows Vista

Erwin

--
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/groups/opt_out.

Rufus Smith wrote:

I really just wanted to comment the use of constants for ID's, but I
don't know
if it's related to the problem.

timer1 = wx.Timer(self, id=1)

instead of id=1, just use id=-1 (or better, id=wx.NewId())and let the
system assign one. Otherwise you may be
choosing an id already in use.

Or just leave it out since the default will do the right thing.

     timer1 = wx.Timer(self)

···

--
Robin Dunn
Software Craftsman