Test/Python(20220101~)
Days28. Timer (최종)
kiostory
2022. 5. 18. 14:06
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 = 20
reps = 0
timer = None
# ---------------------------- TIMER RESET ------------------------------- #
def reset_timer():
window.after_cancel(timer)
Top_label.config(text="Timer", fg=GREEN, bg=YELLOW, font=(FONT_NAME, 50, "bold"))
if WORK_MIN < 10:
pre = "0"
canvas.itemconfig(timer_text, text=f"{pre}{WORK_MIN}:00", fill="white", font=(FONT_NAME, 26, "bold"))
global reps
reps = 0
check_mark.config(text="")
# ---------------------------- TIMER MECHANISM ------------------------------- #
def start_timer():
TIMER = 0
global reps
reps += 1
if reps % 8 == 0:
TIMER = LONG_BREAK_MIN
Top_label.config(text="Break", fg=RED, bg=YELLOW, font=(FONT_NAME, 50, "bold"))
elif reps % 2 == 0:
TIMER = SHORT_BREAK_MIN
Top_label.config(text="Break", fg=PINK, bg=YELLOW, font=(FONT_NAME, 50, "bold"))
else :
TIMER = WORK_MIN
Top_label.config(text="Work", fg=GREEN, bg=YELLOW, font=(FONT_NAME, 50, "bold"))
count_down(TIMER * 60)
# ---------------------------- COUNTDOWN MECHANISM ------------------------------- #
# import time
# counter = 5
#
# while True:
# time.sleep(1)
# counter -= 1
# 이벤트 드리븐(Event Driven)... 메인루프상 계속 모니터링이 되어야 한다. 위의 기능이 실행조차 되지 않는다.
# 다른 방법을 써야 한다. >> window.after()
def count_down(count):
count_min = math.floor(count / 60)
if count_min < 10:
count_min = f"0{count_min}"
count_sec = count % 60
if count_sec < 10:
count_sec = f"0{count_sec}"
canvas.itemconfig(timer_text, text=f"{count_min}:{count_sec}")
if count > 0:
global timer
timer = window.after(1000, count_down, count - 1)
else :
start_timer()
marks = ""
for _ in range(math.floor(reps/2)):
marks += "♥"
check_mark.config(text = marks)
# ---------------------------- UI SETUP ------------------------------- #
window = Tk()
window.title("Pomodoro. 14May2022, kio")
window.config(padx=100, pady=50, bg=YELLOW)
Top_label = Label(text="Timer", fg=GREEN, bg=YELLOW, font=(FONT_NAME, 50, "bold"))
Top_label.grid(column=1, row=0)
canvas = Canvas(width=200, height=224, bg=YELLOW, highlightthickness=0)
tomato_img = PhotoImage(file="tomato.png")
canvas.create_image(100,112,image=tomato_img)
pre = ""
if WORK_MIN < 10:
pre = "0"
timer_text = canvas.create_text(102,128,text=f"{pre}{WORK_MIN}:00", fill="white",font=(FONT_NAME, 26, "bold"))
#canvas.pack()
canvas.grid(column=1, row=1)
start_button = Button(text="Start", command=start_timer, highlightthickness=0)
start_button.grid(column=0 , row=3)
reset_button = Button(text="Reset", command=reset_timer, highlightthickness=0)
reset_button.grid(column=3 , row=3)
check_mark = Label(bg=YELLOW, fg=GREEN, font=(FONT_NAME, 20, "bold"))
check_mark.grid(column=1, row=4)
window.mainloop()