1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
import time
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
global tZero,timer,pause
wx.Frame.__init__(self, parent, id, "Timer", wx.DefaultPosition, wx.Size(310, 150),style= wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
#Panel pour affichage
self.panel = wx.Panel(self, -1)
#Init Timer
timer=wx.Timer(self,1)
tZero=time.time() #Récupération de tZero
pause=0
#Boutons
self.buttonStart = wx.Button(self.panel,-1,"START/RESUME", pos=(25,93))
self.Bind(wx.EVT_BUTTON, self.Start, self.buttonStart)
self.buttonStop = wx.Button(self.panel,-1,"STOP/PAUSE" , pos=(190,93))
self.Bind(wx.EVT_BUTTON, self.Stop, self.buttonStop)
# Fonctions
def Timer(self,evt):
global tZero,timer,tpause
if pause==0:
t=time.time()-tZero # Temps après tZero
else:
t=time.time()-tpause
# Crée un temps en seconde 4.84287779592 (0=01/01/1970)
tiTuple=time.gmtime(t)
# Conversion en tuple (1970, 1, 1, 0, 0, 4, 3, 1, 0)
reste=t-tiTuple[3]*3600.0-tiTuple[4]*60.0-tiTuple[5]*1.0
# Recupération du reste
resteS=("%.2f" % reste )[-2::]
#Affiche les dixièmes et centièmes de l'arrondi ex: 84
tt=time.strftime("%M:%S", tiTuple)+"."+resteS
affich=wx.StaticText(self.panel, -1,str(tt))
affich.CenterOnParent() #Mets l'affichage du chrono au centre du panel
affich.SetLabel(tt)
print tt
def Start(self,evt):
timer.Start(50)
self.Bind(wx.EVT_TIMER, self.Timer, timer)
evt.Skip()
def Stop(self,evt):
global tpause
tpause=time.time()
pause=1
timer.Stop()
evt.Skip()
def on_close(evt):#Pour kill le timer si on ferme la frame sans stoper le chrono
timer.Stop()
frame.Destroy()
wx.EVT_CLOSE(frame, on_close)
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, None)
frame.Show(True)
frame.Centre()
return True
if __name__=='__main__':
app = MyApp(0)
app.MainLoop() |