Skip to content

Commit f8daeaf

Browse files
author
bharat.soni
committed
Added pomodoro timer project written in python
1 parent 27dfb8c commit f8daeaf

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

P/pomodoro-timer/main.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import math
2+
from tkinter import *
3+
4+
# ---------------------------- CONSTANTS ------------------------------- #
5+
PINK = "#e2979c"
6+
RED = "#e7305b"
7+
GREEN = "#9bdeac"
8+
YELLOW = "#f7f5dd"
9+
FONT_NAME = "Courier"
10+
WORK_MIN = 25
11+
SHORT_BREAK_MIN = 5
12+
LONG_BREAK_MIN = 20
13+
reps = 0
14+
timer = None
15+
16+
17+
# ---------------------------- TIMER RESET ------------------------------- #
18+
def reset_timer():
19+
window.after_cancel(timer)
20+
canvas.itemconfig(timer_text, text="00:00")
21+
timer_label.config(text="Timer", fg=GREEN)
22+
check_mark.config(text="")
23+
global reps
24+
reps = 0
25+
26+
27+
# ---------------------------- TIMER MECHANISM ------------------------------- #
28+
def start_timer():
29+
global reps
30+
reps += 1
31+
work_sec = WORK_MIN * 60
32+
short_break_sec = SHORT_BREAK_MIN * 60
33+
long_break_sec = LONG_BREAK_MIN * 60
34+
35+
# 1 3 5 7 work
36+
# 2 4 6 short break
37+
# 8 long break
38+
39+
if reps % 8 == 0:
40+
timer_label.config(text="Break", fg=RED)
41+
count_down(long_break_sec)
42+
elif reps % 2 == 0:
43+
timer_label.config(text="Break", fg=PINK)
44+
count_down(short_break_sec)
45+
else:
46+
timer_label.config(text="Work", fg=GREEN)
47+
count_down(work_sec)
48+
49+
50+
# ---------------------------- COUNTDOWN MECHANISM ------------------------------- #
51+
def count_down(count):
52+
count_min = math.floor(count / 60)
53+
count_sec = count % 60
54+
if count_min < 10:
55+
count_min = f"0{count_min}"
56+
if count_sec < 10:
57+
count_sec = f"0{count_sec}"
58+
canvas.itemconfig(timer_text, text=f"{count_min}:{count_sec}")
59+
if count > 0:
60+
global timer
61+
timer = window.after(1000, count_down, count - 1)
62+
else:
63+
start_timer()
64+
marks = ""
65+
global reps
66+
work_sessions = math.floor(reps / 2)
67+
for _ in range(work_sessions):
68+
marks += "✔"
69+
check_mark.config(text=marks)
70+
71+
72+
# ---------------------------- UI SETUP ------------------------------- #
73+
74+
window = Tk()
75+
window.title("pomodoro")
76+
window.config(padx=100, pady=50, bg=YELLOW)
77+
timer_label = Label(text="Timer", font=(FONT_NAME, 30, "bold"), fg=GREEN, bg=YELLOW, highlightthickness=0)
78+
timer_label.grid(column=1, row=0)
79+
80+
canvas = Canvas(width=203, height=224, bg=YELLOW, highlightthickness=0)
81+
image = PhotoImage(file="tomato.png")
82+
canvas.create_image(103, 112, image=image)
83+
timer_text = canvas.create_text(103, 130, text="00:00", fill="white", font=(FONT_NAME, 35, "bold"))
84+
canvas.grid(column=1, row=1)
85+
86+
start_btn = Button(text="Start", highlightthickness=0, command=start_timer)
87+
start_btn.grid(column=0, row=2)
88+
89+
check_mark = Label(text="")
90+
check_mark.config(fg=GREEN, bg=YELLOW, highlightthickness=0)
91+
check_mark.grid(column=1, row=3)
92+
93+
reset_btn = Button(text="Reset", highlightthickness=0, command=reset_timer)
94+
reset_btn.grid(column=2, row=2)
95+
96+
window.mainloop()

P/pomodoro-timer/tomato.png

10.5 KB
Loading

0 commit comments

Comments
 (0)