-
Notifications
You must be signed in to change notification settings - Fork 9
/
pomodoro.py
145 lines (117 loc) · 5.05 KB
/
pomodoro.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import pyttsx3 as text_to_speech
from tkinter import Button, Canvas, Label, Tk, PhotoImage
from tkinter.constants import LEFT, TOP
from pomodoro_settings import PomodoroAppSettings
class PomodoroTimerApp(Tk):
def __init__(self):
super().__init__()
self.repetition_number = 0
self.clicked_started_button_count = 0
self.timer_count = None
self.settings = PomodoroAppSettings()
self.tomato_image = PhotoImage(file=self.settings.TOMATO_IMAGE_PATH)
self.work_break_label = Label(text='',
fg=self.settings.GREEN,
bg=self.settings.LIGHT_RED,
font=self.settings.WORK_BREAK_LABEL)
self.work_break_label.pack(side=TOP, pady=0)
self.canvas = Canvas(width=600, height=600)
self.setup_text_to_speech()
self.setup_pomodoro_screen()
self.setup_canvas_screen()
self.timer_label = self.add_timer_label_for_canvas(
300, 330, text='25:00', fill='#FFF', font=self.settings.TIMER_FONT)
self.setup_buttons()
def setup_text_to_speech(self):
self.engine = text_to_speech.init()
voices = self.engine.getProperty('voices')
self.engine.setProperty('voice', voices[1].id)
def click_start_button(self):
# break out if user clicks the start button multiples times
if self.clicked_started_button_count == 1:
return
self.clicked_started_button_count += 1
self.start_timer()
def setup_buttons(self):
self.add_start_button()
self.add_reset_button()
def add_start_button(self):
button = Button(self,
text="Start",
borderwidth=0,
activeforeground=self.settings.LIGHT_RED,
background='white',
font=self.settings.START_BUTTON_FONT,
command=self.click_start_button)
button.pack(side=LEFT, padx=100)
def add_reset_button(self):
button = Button(self,
text="Reset",
borderwidth=0,
activeforeground=self.settings.LIGHT_RED,
background='white',
font=self.settings.START_BUTTON_FONT,
command=self.reset_timer)
button.pack(side=TOP)
def setup_pomodoro_screen(self):
width = self.winfo_screenwidth()
add_spaces = (" " * (int(width) // 50))
self.title(f"{add_spaces}Big Pomodoro Timer")
self.config(padx=100, pady=50, background='#F6CFCF')
self.resizable(width=False, height=False)
def reset_timer(self):
if self.timer_count:
self.after_cancel(self.timer_count)
self.canvas.itemconfigure(self.timer_label, text="25:00")
self.work_break_label.config(text="")
self.repetition_number = 0
self.clicked_started_button_count = 0
self.timer_count = None
def start_timer(self):
self.repetition_number += 1
rep_num = self.repetition_number
# long break of 25 min
if rep_num % 8 == 0:
text = "It's time for a big break!"
self.work_break_label.config(text="It's time for a big break!",
fg=self.settings.RED)
self.count_down(self.settings.LONG_BREAK_MINUTES)
text_to_speech.speak(text)
# short break 5 min
elif rep_num % 2 == 0:
text = "It's time for a break!"
self.work_break_label.config(text=text, fg=self.settings.RED)
self.count_down(self.settings.SHORT_BREAK_MINUTES)
text_to_speech.speak(text)
else:
# work time
text = "It's time to work!"
text_to_speech.speak(text)
self.work_break_label.config(text=text, fg=self.settings.GREEN)
self.count_down(self.settings.WORK_MINUTES)
def count_down(self, count):
if count == -1:
self.start_timer()
else:
minutes = count // 60
seconds = count % 60
if count < 10:
seconds = f"0{seconds}"
elif int(seconds) < 10:
seconds = f"0{seconds}"
timer = f"{minutes}:{seconds}"
self.canvas.itemconfigure(self.timer_label, text=timer)
self.timer_count = self.after(1000, self.count_down, count - 1)
def setup_canvas_screen(self):
self.canvas.config(background=self.settings.LIGHT_RED,
highlightthickness=0)
self.pack_items_to_canvas()
def pack_items_to_canvas(self):
self.add_tomato_image_to_canvas()
self.canvas.pack()
def add_timer_label_for_canvas(self, x, y, text, fill, font):
return self.canvas.create_text(x, y, text=text, fill=fill, font=font)
def kill_app(self):
self.destroy()
def add_tomato_image_to_canvas(self):
self.canvas.create_image(300, 300, image=self.tomato_image)