From 79ef354b73f2d40e48fb47d51288d8896b3414ac Mon Sep 17 00:00:00 2001 From: renzon Date: Tue, 11 Apr 2017 16:32:17 -0300 Subject: [PATCH] Adicionado exemplo do Timer Isso fica de acordo com link nos slides do curso --- exemplos/__init__.py | 0 exemplos/timer.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 exemplos/__init__.py create mode 100644 exemplos/timer.py diff --git a/exemplos/__init__.py b/exemplos/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/exemplos/timer.py b/exemplos/timer.py new file mode 100644 index 0000000..ae7db19 --- /dev/null +++ b/exemplos/timer.py @@ -0,0 +1,36 @@ +from tkinter import Frame, Label, Button + + +class Timer(Frame): + def __init__(self): + super().__init__() + self.inicio = self.agora = 15 + self.pendente = None # alarme pendente + self.grid() + self.mostrador = Label( + self, width=2, anchor='e', font='Helvetica 120 bold') + self.mostrador.grid(column=0, row=0, sticky='nswe') + self.bt_start = Button(self, text='Start', command=self.start) + self.bt_start.grid(column=0, row=1, sticky='we') + self.atualizar_mostrador() + + def atualizar_mostrador(self): + self.mostrador['text'] = str(self.agora) + + def start(self): + if self.pendente: + self.after_cancel(self.pendente) + self.agora = self.inicio + self.atualizar_mostrador() + self.pendente = self.after(1000, self.tictac) + + def tictac(self): + self.agora -= 1 + self.atualizar_mostrador() + if self.agora > 0: + self.pendente = self.after(1000, self.tictac) + + +if __name__ == '__main__': + timer = Timer() + timer.mainloop()