From 33b7dbabd8faf4e70465bb4bb71ab606b2cf1100 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Sun, 15 Apr 2018 03:30:31 +0400 Subject: [PATCH] chrono.py: ajouter le bouton pause --- chrono.py | 125 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 94 insertions(+), 31 deletions(-) diff --git a/chrono.py b/chrono.py index 03d1d88..8f3f12d 100755 --- a/chrono.py +++ b/chrono.py @@ -83,13 +83,24 @@ def parse_timeout(s): return h * 3600 + m * 60 + s class Chrono: + MODE_MINUTEUR = 'M' + MODE_CHRONOMETRE = 'C' + STATE_STARTED = 'started' + STATE_PAUSED = 'paused' + STATE_STOPPED = 'stopped' + + ZERO = None + + state = None + mode = None timeout = None + elapsed = None date_start = None - date_end = None + max_elapsed = None initial = None - started = None def __init__(self, timeout=None, start=False): + self.ZERO = self.__delta(0) self.set_timeout(timeout) if start: self.start() @@ -113,31 +124,68 @@ class Chrono: timeout = tmp if timeout == 0: timeout = None self.timeout = timeout - if timeout is None: self.initial = '00:00' - else: self.initial = self.__format(self.__delta(timeout)) + self.elapsed = self.ZERO + self.state = self.STATE_STOPPED + if timeout is None: + self.mode = self.MODE_CHRONOMETRE + self.initial = self.__format(self.ZERO) + else: + self.mode = self.MODE_MINUTEUR + self.max_elapsed = self.__delta(timeout) + self.initial = self.__format(self.max_elapsed) + + def is_chronometre(self): + return self.mode == self.MODE_CHRONOMETRE + + def is_minuteur(self): + return self.mode == self.MODE_MINUTEUR + + def get_elapsed(self): + if self.date_start is None: return self.ZERO + delta = Datetime.today() - self.date_start + return self.elapsed + delta def start(self, timeout=None): if timeout is None: timeout = self.timeout + self.elapsed = self.ZERO self.date_start = Datetime.today() - if timeout is None: self.date_end = None - else: self.date_end = self.date_start + self.__delta(timeout) - self.started = True - - def stop(self): - self.started = False + self.state = self.STATE_STARTED def is_started(self): - return self.started + return self.state == self.STATE_STARTED + + def pause(self): + if self.state == self.STATE_PAUSED: + self.date_start = Datetime.today() + self.state = self.STATE_STARTED + else: + self.elapsed = self.get_elapsed() + self.state = self.STATE_PAUSED + + def is_paused(self): + return self.state == self.STATE_PAUSED + + def stop(self): + self.elapsed = self.get_elapsed() + self.state = self.STATE_STOPPED + + def is_stopped(self): + return self.state == self.STATE_STOPPED def is_end(self): - return self.started and self.date_end is not None and Datetime.today() >= self.date_end + if not self.is_started(): return False + if not self.is_minuteur(): return False + elapsed = self.get_elapsed() + return elapsed >= self.max_elapsed def __repr__(self): - now = Datetime.today() - if self.date_end is None: delta = now - self.date_start - elif now > self.date_end: delta = Timedelta() - else: delta = self.date_end - now - return self.__format(delta) + elapsed = self.get_elapsed() if self.is_started() else self.elapsed + if self.is_minuteur(): + delta = self.max_elapsed - elapsed + if delta < self.ZERO: delta = self.ZERO + return self.__format(delta) + else: + return self.__format(elapsed) def run_chronometre(timeout=None, autostart=False): from Tkinter import Tk, Toplevel, Frame, Label, Entry, Button @@ -217,11 +265,9 @@ def run_chronometre(timeout=None, autostart=False): class Application(Frame): root = None chrono = None - stop = None def __init__(self, timeout=None, autostart=False, **kw): self.chrono = Chrono(timeout) - self.stop = False root = Tk() root.title("Chronomètre") @@ -229,6 +275,8 @@ def run_chronometre(timeout=None, autostart=False): root.rowconfigure(0, weight=1) root.bind("c", lambda event: self.do_config()) root.bind("s", lambda event: self.do_start()) + root.bind("p", lambda event: self.do_pause()) + root.bind("", lambda event: self.do_pause()) root.bind("q", lambda event: self.quit()) self.root = root @@ -236,19 +284,22 @@ def run_chronometre(timeout=None, autostart=False): Frame.__init__(self, **kw) self.TIME = Label(self, width=10, height=2, text=self.chrono.initial, padx=30, pady=10, font=('Helvetica', 18, "normal")) - self.START = Button(self, text="Démarrer", command=self.do_start) + self.START = Button(self, text="Start", command=self.do_start) + self.PAUSE = Button(self, text="Pause", command=self.do_pause, state="disabled") self.CONFIG = Button(self, text="Config", command=self.do_config) - self.QUIT = Button(self, text="Quitter", command=self.quit) + self.QUIT = Button(self, text="Quit", command=self.quit) self.grid(column=0, row=0, sticky='nsew') - self.TIME.grid(column=0, row=0, columnspan=3, sticky='nsew') + self.TIME.grid(column=0, row=0, columnspan=4, sticky='nsew') self.START.grid(column=0, row=1, sticky='ew') - self.CONFIG.grid(column=1, row=1, sticky='ew') - self.QUIT.grid(column=2, row=1, sticky='ew') + self.PAUSE.grid(column=1, row=1, sticky='ew') + self.CONFIG.grid(column=2, row=1, sticky='ew') + self.QUIT.grid(column=3, row=1, sticky='ew') self.columnconfigure(0, weight=2) - self.columnconfigure(1, weight=1) - self.columnconfigure(2, weight=2) + self.columnconfigure(1, weight=2) + self.columnconfigure(2, weight=1) + self.columnconfigure(3, weight=1) self.rowconfigure(0, weight=1) if autostart: self.do_start() @@ -263,15 +314,27 @@ def run_chronometre(timeout=None, autostart=False): self.root.after(300, self.update_time) def do_start(self): + self.PAUSE.configure(state="normal") + self.START.configure(text="reStart") self.chrono.start() self.update_time() + def do_pause(self): + self.chrono.pause() + if self.chrono.is_paused(): + self.PAUSE.configure(text="unPause") + else: + self.PAUSE.configure(text="Pause") + self.update_time() + def do_config(self): chrono = self.chrono chrono.stop() config = Config(self.root) if config.have_result: try: + self.PAUSE.configure(text="Pause", state="disabled") + self.START.configure(text="Start") chrono.set_timeout(config.result) self.TIME.configure(text=chrono.initial) except: @@ -320,14 +383,14 @@ if __name__ == '__main__': AP.set_defaults(autostart=None, timeout=None) AP.add_argument('timeout', metavar='TIMEOUT', nargs='?', help=u"""\ ->>> (valeur vide) +>>> '' (valeur vide) chronomètre qui démarre à 0:00 et ne s'arrête pas ->>> H:M:S (heures:minutes:secondes) ->>> ou M:S (minutes:secondes) ->>> ou M (minutes) +>>> 'H:M:S' (heures:minutes:secondes) +>>> ou 'M:S' (minutes:secondes) +>>> ou 'M' (minutes) minuteur qui démarre à H:M:S et fait un décompte jusqu'à 0:00. A la fin du décompte, une sonnerie retentit. ->>> @H[:M[:S]] +>>> '@H[:M[:S]]' minuteur qui fonctionne comme précédemment, sauf qu'on spécifie l'heure d'arrivée, et que la durée est calculée automatiquement""") AP.add_argument('-n', '--no-autostart', dest='autostart', action='store_false',