I have here a very basic timer, just a standalone working snippet of a large project.
It takes an integer (15 in this demo) and counts down in seconds to 0.
The display is H:M:S. As the integer will never be higher than 20 the display is largely redundant, it really needs to be: mins:secs:10ths. Can this be achieved? Thanks.
import time
import wx
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title='Timer')
panel = wx.Panel(self)
self.countdown = 15 #seconds
lbl = '00:00:' + str(self.countdown)
font = wx.Font(24, wx.FONTFAMILY_ROMAN,
wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_BOLD)
self.lbl = wx.StaticText(panel, label=lbl)
self.lbl.SetFont(font)
btn = wx.Button(panel, label='Start Countdown')
btn.Bind(wx.EVT_BUTTON, self.start)
self.timer1 = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.prevTimer, self.timer1)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.lbl, 0, wx.ALL, 5)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
self.Show()
def start(self, event):
self.timer1.Start(1000)
def prevTimer(self, evt):
self.countdown -= 1
seg = time.strftime("%H:%M:%S", time.gmtime(self.countdown))
if seg == "00:00:00": self.timer1.Stop()
self.lbl.SetLabel(str(seg))
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame()
app.MainLoop()