2 timers, doesnt work?

Discussion in 'Python' started by Kiran, Jul 25, 2006.

  1. Kiran

    Kiran Guest

    I am creating 2 timers inside a GUI, but it seems that only the one
    declared last (the second timer), gets triggered, but the first one
    doesnt.

    Here is the code for the timers:
    # main timer, which governs when the register watcher get updated
    self.mainTimer = wx.Timer(self)
    self.Bind(wx.EVT_TIMER, self.UpdateAll, self.mainTimer)
    self.mainTimer.Start(5, False)

    # graph timer, which governs when the graph gets updated
    self.grphTimer = wx.Timer(self)
    self.Bind(wx.EVT_TIMER, self.upGrphDisplay, self.grphTimer)
    self.grphTimer.Start(101, False)

    So in this case, only the upGrphDisplay method gets called, the
    UpdateAll function doesnt get called.

    Any clues as to why this is happening?

    thanks a lot!
    -- kiran
    Kiran, Jul 25, 2006
    #1
    1. Advertising

  2. Kiran wrote:
    > I am creating 2 timers inside a GUI, but it seems that only the one
    > declared last (the second timer), gets triggered, but the first one
    > doesnt.
    >


    You really should check the archives before posting. Exactly the same
    question was asked less than a week ago.

    The original question was answered by Nikie. I have quoted the reply
    verbatim -

    ----------------------------------------------------------------
    The problem is not that the first timer is stopped, the problem is
    that both timers happen to call the same method in the end.

    Think of the "Bind" method as an assignment: it assigns a handler
    function to an event source. If you call it twice for the same event
    source, the second call will overwrite the first event handler. That's
    what happens in your code.


    The easiest way to change this is by using different ids for the
    timers:


    def startTimer1(self):
    self.t1 = wx.Timer(self, id=1)
    self.t1.Start(2000)
    self.Bind(wx.EVT_TIMER, self.OnUpTime, id=1)


    def startTimer2(self):
    self.t2 = wx.Timer(self, id=2)
    self.t2.Start(1000)
    self.Bind(wx.EVT_TIMER, self.OnTime, id=2)


    This way, the timers launch two different events, which are bound to
    two different methods.
    ----------------------------------------------------------------

    I would suggest one minor change. Create a variable for the id using
    wx.NewId(), and pass the variable as an argument in both places,
    instead of hard-coding the id. That way you are guaranteed to get a
    unique id. In a large project it can be difficult to keep track of
    which id's have been used if they are all hard-coded.

    Frank Millman
    Frank Millman, Jul 26, 2006
    #2
    1. Advertising

  3. Kiran

    Kiran Guest

    Makes sense Frank, and that seemed to work also, so thanks a lot!


    Frank Millman wrote:
    > Kiran wrote:
    > > I am creating 2 timers inside a GUI, but it seems that only the one
    > > declared last (the second timer), gets triggered, but the first one
    > > doesnt.
    > >

    >
    > You really should check the archives before posting. Exactly the same
    > question was asked less than a week ago.
    >
    > The original question was answered by Nikie. I have quoted the reply
    > verbatim -
    >
    > ----------------------------------------------------------------
    > The problem is not that the first timer is stopped, the problem is
    > that both timers happen to call the same method in the end.
    >
    > Think of the "Bind" method as an assignment: it assigns a handler
    > function to an event source. If you call it twice for the same event
    > source, the second call will overwrite the first event handler. That's
    > what happens in your code.
    >
    >
    > The easiest way to change this is by using different ids for the
    > timers:
    >
    >
    > def startTimer1(self):
    > self.t1 = wx.Timer(self, id=1)
    > self.t1.Start(2000)
    > self.Bind(wx.EVT_TIMER, self.OnUpTime, id=1)
    >
    >
    > def startTimer2(self):
    > self.t2 = wx.Timer(self, id=2)
    > self.t2.Start(1000)
    > self.Bind(wx.EVT_TIMER, self.OnTime, id=2)
    >
    >
    > This way, the timers launch two different events, which are bound to
    > two different methods.
    > ----------------------------------------------------------------
    >
    > I would suggest one minor change. Create a variable for the id using
    > wx.NewId(), and pass the variable as an argument in both places,
    > instead of hard-coding the id. That way you are guaranteed to get a
    > unique id. In a large project it can be difficult to keep track of
    > which id's have been used if they are all hard-coded.
    >
    > Frank Millman
    Kiran, Aug 1, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rad
    Replies:
    0
    Views:
    536
  2. Showjumper
    Replies:
    5
    Views:
    432
    Showjumper
    Jul 17, 2003
  3. How do timers work?

    , Feb 22, 2005, in forum: Java
    Replies:
    9
    Views:
    1,121
    Chris Uppal
    Feb 23, 2005
  4. Janaka Perera
    Replies:
    0
    Views:
    1,103
    Janaka Perera
    Oct 29, 2007
  5. Replies:
    3
    Views:
    417
    alex23
    May 27, 2008
Loading...

Share This Page