-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_timer.py
More file actions
61 lines (55 loc) · 2.41 KB
/
start_timer.py
File metadata and controls
61 lines (55 loc) · 2.41 KB
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
from time import strftime,gmtime
from tkinter import Canvas, Button
from flash_card import FlashCard
class Timer():
def __init__(self, window, canvas, main_class):
self.BACKGROUND = "white"
self.seconds = 5
self.window = window
self.canvas = canvas
self.words_combo = {}
self.main_class = main_class
self.top_canvas = None
self.timer_id = None
self.TIMER_FONT = ("Serif", 10, "bold")
self.flashcard = FlashCard(self.window, self.canvas, main_class= self.main_class)
def timer_ui(self)->None:
"""_Create a Canvas where to display the timer and a Button to start the countdown_
"""
button = Button(self.window, text="start", command=self.start_timer)
button.grid(row=0, column=0,)
self.top_canvas = Canvas(self.window, width= 50, height= 20,bg=self.BACKGROUND)
self.top_canvas.grid(row= 0, column= 2,)
self.text_id =self.top_canvas.create_text(25,10, text="00", font= self.TIMER_FONT)
def start_timer(self)->None:
"""_Start the timer if there are more cards to show_
"""
if self.flashcard.check_for_cards():
self.words_combo =self.flashcard.get_card()
self.flashcard.display_front_card()
self.run_timer(self.seconds)
def run_timer(self, seconds: int) ->None:
"""_Rundown time and when time gets to zero , display the back card, check if there are more cards and restart the timer_
Args:
seconds (int): _How many seconds to count down _
"""
time = strftime("%S", gmtime(seconds))
self.top_canvas.itemconfig(self.text_id,text=f"{time}")
if seconds > 0:
self.timer_id = self.window.after(1000, self.run_timer, seconds - 1)
else:
self.flashcard.display_back_card()
if self.flashcard.check_for_cards():
self.window.after(4000,self.start_timer)
def cancel_current(self)->None:
"""_Cancel the current sequence of countdown_
"""
self.window.after_cancel(self.timer_id)
self.canvas.delete("all")
self.start_timer()
def check_if_to_save(self)-> bool:
"""_Check if its time to save the word_to_learn dict to csv_
Returns:
bool: _True if all cards shown else False_
"""
return self.flashcard.counter == self.flashcard.length