salut,
je veux faire une sorte de chronométre sur mon interface qui commence lorsque j'appuis sur un bouton et s'arrete lorsque une fonction corespandante au bouton fini l'execution. je ne sait pas comment faire:?:?:? des idées!8O
Version imprimable
salut,
je veux faire une sorte de chronométre sur mon interface qui commence lorsque j'appuis sur un bouton et s'arrete lorsque une fonction corespandante au bouton fini l'execution. je ne sait pas comment faire:?:?:? des idées!8O
Bonjour,
pour pouvoir faire un chronometre, tu vas devoir utiliser les threads et les évenements personnalisés.
Regarde ce code source: http://python.developpez.com/sources...icatorCompress, tu peux t'en inspirer.
Bonjour,
J'avais développé un chrono à partir de wx.
La gestion du temps n'est pas dans un thread mais est liée à l'horloge de l'ordinateur.
Code:
1
2 self.tZero=time.time() #Récupération de tZero self.t=time.time() -self.tZero # Temps après tZero
L'affichage se faisant lors du déclenchement d'un timer (c'est à dire une fonction qui se lance toute les n millisecondes).
Code:frame_1.timer.Start(50) #temps maj en millisecondes
Il reste à développer la notion de bouton dans l'interface ainsi que la gestion de la marche arrêt.
Alex
Code:
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 #!/usr/bin/env python # -*- coding: iso-8859-15 -*- import wx import time class MyFrame(wx.Frame): def __init__(self, *args, **kwds): kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) self.tAff = wx.StaticText(self, -1, "00:00:00,00") self.tZero=time.time() #Récupération de tZero self.t=time.time() -self.tZero # Temps après tZero self.timer=wx.Timer(self, 1) self.tailleText=100 self.__set_properties() self.__do_layout() def __set_properties(self): self.SetTitle("frame_1") self.tAff.SetBackgroundColour(wx.Colour(50, 153, 204)) self.tAff.SetFont(wx.Font(self.tailleText, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "")) self.Bind(wx.EVT_TIMER, self.onTimer, self.timer) def __do_layout(self): sizer_1 = wx.BoxSizer(wx.HORIZONTAL) sizer_1.Add(self.tAff, 0, 0, 0) self.SetSizer(sizer_1) sizer_1.Fit(self) self.Layout() def onTimer(self, event): # Timer self.t=time.time() -self.tZero # Crée un temps en seconde 4.84287779592 (0=01/01/1970) tiTuple=time.gmtime(self.t) # Conversion en tuple (1970, 1, 1, 0, 0, 4, 3, 1, 0) reste=self.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("%H:%M:%S", tiTuple)+","+resteS self.tAff.SetLabel(tt) if __name__ == "__main__": app = wx.PySimpleApp(0) wx.InitAllImageHandlers() frame_1 = MyFrame(None, -1, "") app.SetTopWindow(frame_1) frame_1.Show() frame_1.timer.Start(50) #temps maj en millisecondes app.MainLoop()
J'en ai fais un aussi.
Mais n'y de thread, ni besoin du système.
Tout passe par un wx.Timer !
Pour le stopper suffit d'appeler la méthode Stop de l'objet self.timer, pour le remettre à zéro mettre la variable self.spend_time à 0...Code:
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
76
77 #!/usr/bin/env python # -*- coding: utf-8 -*- import wx import os, sys class Gui (wx.Frame): def __init__ (self, titre): wx.Frame.__init__(self, None, wx.ID_ANY, title=titre, style=wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX) icone = wx.Icon(self.abs_path_data() + 'data/Icon.ico', wx.BITMAP_TYPE_ICO) wx.Frame.SetIcon(self, icone) self.panel = wx.Panel(self, wx.ID_ANY) sizer = wx.BoxSizer(wx.VERTICAL) # ----------------------------------------------------------------------------------------------------------- Widget self.text = wx.StaticText(self.panel, wx.ID_ANY, u'00:00:00') font = wx.Font(40, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD) self.text.SetFont(font) sizer.Add(self.text, flag=wx.ALIGN_CENTER|wx.ALL, border=20) # ----------------------------------------------------------------------------------------------------------- Divers # Sizer + Fit self.panel.SetSizerAndFit(sizer) self.Fit() # Events self.Bind(wx.EVT_TIMER, self.label) # Chrono self.spend_time = 0 self.timer = wx.Timer(self) self.timer.Start(1000) def label (self, event): self.spend_time += 1 temps = self.spend_time # ---------------------------------- Calculs heure = temps / 3600 temps = temps - (heure * 3600) minute = temps / 60 temps = temps - (minute * 60) seconde = temps # ---------------------------------- self.text.SetLabel(str(heure).zfill(2) + ':' + str(minute).zfill(2) + ':' + str(seconde).zfill(2)) self.SetClientSize(self.panel.GetBestSize()) def abs_path_data (self): try: root = __file__ except: sys.exit() else: if os.path.islink(root): root = os.path.realpath(root) return os.path.dirname(os.path.abspath(root)) + os.sep class Ihm (wx.App): def OnInit (self): root = Gui(u'PyChronos') root.Show(True) self.SetTopWindow(root) return True if __name__ == '__main__': app = Ihm() app.MainLoop()
Amuse toi bien :) !