본문 바로가기

Python 🎧

[python] 파이썬 포모도로 타이머 만들기 - tkinter/window/after/label/grid

이상하게 근 2주간은 집중이 안되더라

특히, 포모도로 프로그램 공부할때는

더더욱이 집중이 안돼서 시간이 엄청 오래걸렸다

 

그럴때 포모도로를 이용해서 집중해버리기 🍅

 

이태리에서는 토마토 라는 뜻이라는데....

머 암튼 ㄱㄱ 

 

🍅 포모도로 코드

import math
import tkinter

# ---------------------------- 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
chk = ""
timer = None
# ---------------------------- TIMER RESET ------------------------------- # 
def reset_timer():
    window.after_cancel(timer)
    canvas.itemconfig(timer_text,text="00:00")
    label.config(text="Timer")
    global reps, chk
    reps = 0
    chk = ""
    chk_label.config(text=chk)

# ---------------------------- TIMER MECHANISM ------------------------------- # 

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:
        label.config(text="break☕️", fg=RED)
        count_down(long_break_sec)
    elif reps%2==0:
        label.config(text="break☕️", fg=PINK)
        count_down(short_break_sec)
    else :
        label.config(text="work👁️", fg=GREEN)
        count_down(work_sec)


# ---------------------------- COUNTDOWN MECHANISM ------------------------------- # 
def count_down(cnt) :
    global chk
    min = math.floor(cnt/60)
    sec = cnt%60
    # 00 : 00 형식으로 시간 보이게
    canvas.itemconfig(timer_text, text= f"{format(min,'02d')}:{format(sec,'02d')}")
    if cnt > 0 :
        global timer
        timer = window.after(1000,count_down, cnt-1)
    else :
        start_timer()
        if reps %2 == 0:
            chk += "✓"
            chk_label.config(text=chk)

# ---------------------------- UI SETUP ------------------------------- #
window = tkinter.Tk()
window.title("뽀모도로 🍅")
window.config(padx=30,pady=30,bg= YELLOW)

# timer label
label = tkinter.Label(bg=YELLOW,fg=GREEN,font=(FONT_NAME,50,"bold"))
label.config(text="Timer")
label.grid(column=1,row=0)

# tomato canvas
canvas = tkinter.Canvas(width=200, height=224, bg=YELLOW, highlightthickness=0)
tomato_img = tkinter.PhotoImage(file="tomato.png")
canvas.create_image(100,112,image = tomato_img ) # 토마토 위치
timer_text = canvas.create_text(100,130, text = "00:00", fill="white", font=(FONT_NAME,35,"bold"))
canvas.grid(column=1, row=1)

# start button
start_button = tkinter.Button(command=start_timer,highlightthickness=0,bg=YELLOW, bd=0,padx=0, pady=0,width=3, height=2)
start_button.config(text="start")
start_button.grid(column=0, row=2)

# reset button
reset_button = tkinter.Button(command=reset_timer,highlightthickness=0,bg=YELLOW, bd=0,padx=0, pady=0,width=3, height=2)
reset_button.config(text="reset")
reset_button.grid(column=2, row=2)

# chk label
chk_label = tkinter.Label(fg=GREEN,bg=YELLOW,font=(FONT_NAME,30))
chk_label.grid(column=1,row=3)

window.mainloop()

 

🍅 실행 결과