Reputation: 1
I'm trying to create a GUI timer using wxPython. A Start button is clicked, the timer starts and a label displays the timer counting the seconds up to minutes and maybe to hours. A Stop button stops the timer. I don't know how to have the timer constantly displayed in the label. I tried a while True loop but it seems SetLabel() or Time() wants to display once and is waiting for an end to the loop.
import wx
class Timer(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(400, 350))
self.main()
self.Centre()
self.Show()
def main(self):
panel = wx.Panel(self)
sizer = wx.GridBagSizer(5, 0)
self.timer = wx.StopWatch()
self.label = wx.StaticText(panel)
sizer.Add(self.label, pos=(0, 0), flag=wx.ALIGN_CENTER)
button_start = wx.Button(panel, label='Start')
self.Bind(wx.EVT_BUTTON, self.OnStart, button_start)
sizer.Add(button_start, pos=(1, 0), flag=wx.ALIGN_CENTER)
button_stop = wx.Button(panel, label='Stop')
self.Bind(wx.EVT_BUTTON, self.OnStop, button_stop)
sizer.Add(button_stop, pos=(2, 0), flag=wx.ALIGN_CENTER)
sizer.AddGrowableRow(0)
sizer.AddGrowableRow(1)
sizer.AddGrowableRow(2)
sizer.AddGrowableCol(0)
panel.SetSizer(sizer)
def OnStart(self, event):
self.timer.Start()
while True:
self.label.SetLabel(str(self.timer.Time()))
def OnStop(self, event):
self.timer.Pause()
if __name__ == '__main__':
app = wx.App()
Timer(None, 'Timer')
app.MainLoop()
Upvotes: 0
Views: 2270
Reputation: 33071
It sounds like you want to start from 00:00:00 and count up, right? You should take a look at the LEDNumberCtrl demo in the wxPython demo. The third example in it is very similar to what you're looking for. I think you could get it to work with minimal fiddling.
Upvotes: 0