DEV Community

Palak Hirave
Palak Hirave

Posted on

Day 28 : Pomodoro Timer

Many of you may have heard of the famous time management technique - the Pomodoro Timer. This is where you set a 'work' timer for 25 minutes and then a 'break' timer for 5 minutes. You repeat this set 4 times before taking a longer 15 minute break. Today, I decided to build that by combining my knowledge about GUIs and the Tkinter module alongside learning about the canvas widget and how dynamic typing in Python works. Overall, this project was straightforward in its steps though I did struggle a bit with figuring out how to smoothly run the timers. As of now, the timers are of 25 mins, with 5 min breaks and then 15 min breaks between the set, but I plan on improving this so that the user can specify their session breaks(with the min requirement of the amount of time being the standard pomodoro, and adding constraints so they don't abuse the break time).

While I cannot upload videos directly into dev.to posts, below are some screenshots of how the final product looks. My code is posted at the bottom as usual.

Screenshots


Work Timer Screen


Short Break Screen


Long Break Screen, keeps on adding check marks after each round of work + break

Program (main.py)

from tkinter import *
import math
# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 15
REPS = 0
timer = None

# ---------------------------- TIMER RESET ------------------------------- # 
def reset_screen():
    global REPS
    REPS = 0
    canvas.itemconfig(title_text, text="Timer", fill=GREEN)
    canvas.itemconfig(timer_text, text="00:00")
    check_marks.config(text=" ")

def reset_timer():
    window.after_cancel(timer)
    reset_screen()

# ---------------------------- TIMER MECHANISM ------------------------------- # 
def timer_time():
    global REPS
    REPS += 1
    if REPS % 8 == 0:
        countdown(LONG_BREAK_MIN * 60)
        canvas.itemconfig(title_text, text="Break", fill=RED)
    elif REPS % 2 == 0:
        countdown(SHORT_BREAK_MIN * 60)
        canvas.itemconfig(title_text, text="Break", fill=PINK)
    else:
        countdown(WORK_MIN * 60)
        canvas.itemconfig(title_text, text="Work", fill=GREEN)

# ---------------------------- COUNTDOWN MECHANISM ------------------------------- # 
def countdown(count):
    global timer
    count_mins = math.floor(count / 60)
    count_secs = count % 60
    if count_secs < 10:
        count_secs = "0" + str(count_secs)

    canvas.itemconfig(timer_text, text=f"{count_mins}:{count_secs}")
    if count > 0:
        timer = window.after(1000, countdown, count - 1)
    else:
        timer_time()
        checks = ""
        for rep in range(math.floor(REPS/2)):
            checks += ""
            check_marks.config(text=checks)

# ---------------------------- UI SETUP ------------------------------- #
window = Tk()
window.title("Pomodoro Timer : Get Work Done")
window.config(padx=100, pady=25, bg = YELLOW)

canvas = Canvas(width=200, height=350, bg=YELLOW, highlightthickness=0)
tomato = PhotoImage(file="tomato.png")

canvas.create_image(100, 180, image=tomato)
canvas.grid(row=1, column=2)

timer_text = canvas.create_text(100, 190, text="00:00", fill="white", font=(FONT_NAME, 35, "bold"))
canvas.grid(row=2, column=2)

title_text = canvas.create_text(100, 30,text="Timer", fill=GREEN, font=(FONT_NAME, 35, "bold"))
canvas.grid(row=2, column=2)

start_button = Button(text = "Start", command=timer_time)
start_button.grid(row=3, column=1)
restart_button = Button(text = "Restart", command=reset_timer)
restart_button.grid(row=3, column=3)

check_marks = Label(text="", font=(FONT_NAME, 10), fg=GREEN, bg=YELLOW)
check_marks.grid(row=3, column=2)


window.mainloop()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)