Day 28 - Tomato Timer Project


Posted by pei_______ on 2022-05-08

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


# constants

from tkinter import *

PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20
timer = None
reps = 0


window = Tk()
window.title("Pomodoro")
window.config(padx=60, pady=20, bg=YELLOW)


tomato_img = PhotoImage(file='tomato.png')
canvas = Canvas(width=201, height=224, bg=YELLOW, highlightthickness=0)
canvas.create_image(100, 112, image=tomato_img)
time_text = canvas.create_text(100, 130, text='00:00', fill='white', font=(FONT_NAME, 35, 'bold'))
canvas.grid(row=2, column=2)

title = Label(text="Timer", bg=YELLOW, fg=GREEN, font=(FONT_NAME, 60, "bold"))
title.grid(row=1, column=2)

start = Button(text="start", command=start_timer)
start.grid(row=4, column=1)

mark = Label(text="", fg=GREEN, bg=YELLOW, font=(FONT_NAME, 20, "bold"))
mark.config(pady=20)
mark.grid(row=5, column=2)

reset = Button(text="reset", command=reset_timer)
reset.grid(row=4, column=3)


window.mainloop()
# time_count function

def time_count(count):
    global timer
    count_min = int(count / 60)
    count_sec = int(count % 60)
    if count_sec < 10:
        count_sec = f'0{count_sec}'

    canvas.itemconfig(time_text, text=f"{count_min}:{count_sec}")
    if count > 0:
        timer = window.after(1000, time_count, count - 1)
    else:
        start_timer()
        marks = ""
        work_sessions = int(reps/2)
        for _ in range(work_sessions):
            marks += "✔"
            mark.config(text=marks)
# start_timer function

def start_timer():
    global reps
    reps += 1

    work_sec = WORK_MIN * 60
    short_break_sec = SHORT_BREAK_MIN * 60
    long_break_sec = LONG_BREAK_MIN * 60

    if reps % 8 == 0:
        time_count(long_break_sec)
        title.config(text='Break', fg=RED)

    elif reps % 2 == 0:
        time_count(short_break_sec)
        title.config(text='Break', fg=PINK)

    else:
        time_count(work_sec)
        title.config(text='Work', fg=GREEN)
# reset_timer function

def reset_timer():
    window.after_cancel(timer)
    title.config(text='Timer', fg=GREEN)
    canvas.itemconfig(time_text, text="00:00")
    mark.config(text="")
    global reps
    reps = 0

#Python #課堂筆記 #100 Days of Code







Related Posts

[Notes] Git How to Step by Step: windows

[Notes] Git How to Step by Step: windows

〈 C++學習日記 #2〉再談指標 Pointer (Part.2)

〈 C++學習日記 #2〉再談指標 Pointer (Part.2)

react-redux 錯誤訊息:...is missing in props validation

react-redux 錯誤訊息:...is missing in props validation


Comments