what's wrong with this if statement?

Discussion in 'Python' started by Philippe Martin, Jul 21, 2006.

  1. John Salerno wrote:

    > Here's the full code, but you can probably safely ignore most of it,
    > especially the wxPython stuff:
    >
    > -----------------------------------
    >
    > import wx
    >
    >
    > class MyFrame(wx.Frame):
    >
    > def __init__(self):
    > wx.Frame.__init__(self, parent=None, id=wx.ID_ANY)
    > panel = wx.Panel(self)
    >
    > mainSizer = wx.BoxSizer(wx.VERTICAL)
    > inputSizer = wx.BoxSizer(wx.HORIZONTAL)
    >
    > self.count = 0
    > self.progress = wx.Gauge(panel, wx.ID_ANY, 100, size=(300, 20))
    > self.status = wx.StaticText(panel, wx.ID_ANY, 'test')
    > prompt = wx.StaticText(panel, wx.ID_ANY, 'Time:')
    > self.input = wx.TextCtrl(panel, wx.ID_ANY, size=(20, 20))
    > self.start = wx.Button(panel, wx.ID_ANY, 'Start')
    > self.timer = wx.Timer(self)
    >
    > mainSizer.Add(self.progress, flag=wx.ALIGN_CENTER | wx.TOP,
    > border=10)
    > mainSizer.Add(self.status, flag=wx.ALIGN_CENTER | wx.ALL,
    > border=10)
    > mainSizer.Add(inputSizer, flag=wx.ALIGN_CENTER)
    > inputSizer.Add(prompt, flag=wx.ALIGN_CENTER)
    > inputSizer.Add(self.input, flag=wx.ALL, border=10)
    > inputSizer.Add(self.start, flag=wx.ALIGN_CENTER)
    >
    > self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
    > self.Bind(wx.EVT_BUTTON, self.OnStart, self.start)
    >
    > panel.SetSizer(mainSizer)
    >
    > def OnStart(self, event):
    > self.time = self.input.GetValue()
    > self.timer.Start(1000)
    >
    > def OnTimer(self, event):
    > self.count += 1
    > if self.count < self.time:
    > self.progress.SetValue(self.count)
    > self.status.SetLabel(str(self.count))
    >
    >
    > class MyApp(wx.App):
    >
    > def OnInit(self):
    > frame = MyFrame()
    > self.SetTopWindow(frame)
    > frame.Show()
    > return True
    >
    >
    > if __name__ == '__main__':
    > app = MyApp(redirect=False)
    > app.MainLoop()
    >
    > ------------------------------------
    >
    > The code in question is mainly this:
    >
    > def OnTimer(self, event):
    > self.count += 1
    > if self.count < self.time:
    > self.progress.SetValue(self.count)
    > self.status.SetLabel(str(self.count))
    >
    > When I run the program, the progress bar and the status label continue
    > to increase even after self.count has (presumably) become larger than
    > self.time. Why is this? All I do is enter in '10' as the value to test
    > it, and it keeps counting even beyond 10.




    Check the type of self.time: unicode - you need to convert it to int
    first ... plus your timer will need to be shutdown when you're done.

    Regards,

    Philippe
     
    Philippe Martin, Jul 21, 2006
    #1
    1. Advertising

  2. Philippe Martin

    John Salerno Guest

    Here's the full code, but you can probably safely ignore most of it,
    especially the wxPython stuff:

    -----------------------------------

    import wx


    class MyFrame(wx.Frame):

    def __init__(self):
    wx.Frame.__init__(self, parent=None, id=wx.ID_ANY)
    panel = wx.Panel(self)

    mainSizer = wx.BoxSizer(wx.VERTICAL)
    inputSizer = wx.BoxSizer(wx.HORIZONTAL)

    self.count = 0
    self.progress = wx.Gauge(panel, wx.ID_ANY, 100, size=(300, 20))
    self.status = wx.StaticText(panel, wx.ID_ANY, 'test')
    prompt = wx.StaticText(panel, wx.ID_ANY, 'Time:')
    self.input = wx.TextCtrl(panel, wx.ID_ANY, size=(20, 20))
    self.start = wx.Button(panel, wx.ID_ANY, 'Start')
    self.timer = wx.Timer(self)

    mainSizer.Add(self.progress, flag=wx.ALIGN_CENTER | wx.TOP,
    border=10)
    mainSizer.Add(self.status, flag=wx.ALIGN_CENTER | wx.ALL,
    border=10)
    mainSizer.Add(inputSizer, flag=wx.ALIGN_CENTER)
    inputSizer.Add(prompt, flag=wx.ALIGN_CENTER)
    inputSizer.Add(self.input, flag=wx.ALL, border=10)
    inputSizer.Add(self.start, flag=wx.ALIGN_CENTER)

    self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
    self.Bind(wx.EVT_BUTTON, self.OnStart, self.start)

    panel.SetSizer(mainSizer)

    def OnStart(self, event):
    self.time = self.input.GetValue()
    self.timer.Start(1000)

    def OnTimer(self, event):
    self.count += 1
    if self.count < self.time:
    self.progress.SetValue(self.count)
    self.status.SetLabel(str(self.count))


    class MyApp(wx.App):

    def OnInit(self):
    frame = MyFrame()
    self.SetTopWindow(frame)
    frame.Show()
    return True


    if __name__ == '__main__':
    app = MyApp(redirect=False)
    app.MainLoop()

    ------------------------------------

    The code in question is mainly this:

    def OnTimer(self, event):
    self.count += 1
    if self.count < self.time:
    self.progress.SetValue(self.count)
    self.status.SetLabel(str(self.count))

    When I run the program, the progress bar and the status label continue
    to increase even after self.count has (presumably) become larger than
    self.time. Why is this? All I do is enter in '10' as the value to test
    it, and it keeps counting even beyond 10.
     
    John Salerno, Jul 21, 2006
    #2
    1. Advertising

  3. Philippe Martin

    John Salerno Guest

    Philippe Martin wrote:

    > Check the type of self.time: unicode - you need to convert it to int
    > first ... plus your timer will need to be shutdown when you're done.


    Ah, of course! Thanks!
     
    John Salerno, Jul 21, 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. Replies:
    3
    Views:
    807
  2. Jay McGavren
    Replies:
    11
    Views:
    1,140
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,094
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    470
    Duncan Booth
    Jul 22, 2004
  5. Replies:
    21
    Views:
    1,060
    Giannis Papadopoulos
    Aug 2, 2005
Loading...

Share This Page