top of page

Pomodoro alarm

  • Writer: 라임 샹큼
    라임 샹큼
  • 4 days ago
  • 2 min read

import tkinter

from tkinter import PhotoImage


# ---------------------------- CONSTANTS ------------------------------- #

PINK = "#e2979c"

RED = "#e7305b"

GREEN = "#9bdeac"

YELLOW = "#f7f5dd"

FONT_NAME = "Courier"

# WORK_MIN = 25

# SHORT_BREAK_MIN = 5

# LONG_BREAK_MIN = 20

WORK_MIN = 0.1

SHORT_BREAK_MIN = 0.1

LONG_BREAK_MIN = 0.1

reps = 0

timer = None

# ---------------------------- TIMER RESET ------------------------------- # 

def reset_timer():

    global reps

    window.after_cancel(timer)

    reps = 0

    title.config(text='Timer', fg=GREEN)

    check_mark.config(text='')

    canvas.itemconfig(timer_text, text=f'00:00')


def raise_above_all(root):

    root.lift()

    root.attributes('-topmost', True)

def lower(root):

    root.after_idle(root.attributes, '-topmost', False)



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

def start_timer():

    global reps

    reps += 1

    work_min = int(WORK_MIN * 60)

    short_break = int(SHORT_BREAK_MIN * 60)

    long_break = int(LONG_BREAK_MIN * 60)


    if reps % 8 ==0:

        count_down(long_break)

        title.config(text = 'Break!',fg = RED)

        raise_above_all(window)

    elif reps %2 ==0:

        count_down(short_break)

        title.config(text = 'Break', fg = PINK)

        raise_above_all(window)

    else:

        count_down(work_min)

        title.config(text = 'Work', fg = GREEN)

        lower(window)



# ---------------------------- COUNTDOWN MECHANISM ------------------------------- # 

def count_down(count):

    count_min = int(count/60)

    count_sec = count % 60


    if count_sec == 0:

        count_sec = '00'

    elif len(str(count_sec)) == 1:

        count_sec = f'0{count_sec}'


    if count_min == 0:

        count_min = '00'

    elif len(str(count_min)) == 1:

        count_sec = f'0{count_min}'


    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()

        check_num = int(reps / 2)

        check_mark.config(text = f'{'✔'*check_num}')

# ---------------------------- UI SETUP ------------------------------- #

window = tkinter.Tk()

window.title('Pomodoro')

window.config(padx = 100, pady = 100, bg=YELLOW)


canvas = tkinter.Canvas(width=200, height = 224,bg=YELLOW,highlightthickness=0)

image = PhotoImage(file = 'tomato.png')

canvas.create_image(99, 112,image = image)

timer_text = canvas.create_text(99,130,text= '00:00',fill = 'white',font = (FONT_NAME,36,'bold'))

canvas.grid(column=1, row=1)


title = tkinter.Label(text='Timer', font=(FONT_NAME,40,'bold'),bg=YELLOW, fg=GREEN)

title.grid(column=1,row=0)


#start button

start_button = tkinter.Button(text='Start',highlightthickness=0, command = start_timer)

start_button.grid(column=0,row = 2)


#reset button

reset_button = tkinter.Button(text = 'Reset', highlightthickness=0,bg = YELLOW, command= reset_timer)

reset_button.grid(column = 2, row = 2)


check_mark = tkinter.Label(bg = YELLOW,fg = GREEN)

check_mark.grid(column = 1,row = 3)


window.mainloop()


What I learned

In tkinter, you can put image on screen like with turtle using the canvas widget

canvas = tkinter.Canvas()

img = Photoimage(file = filename)

canvas.create_image(x,y position, img)

canvas.pack()

 
 
 

Recent Posts

See All
Password Manager

import tkinter from tkinter import messagebox from tkinter import PhotoImage import random # ---------------------------- PASSWORD...

 
 
 
NATO alphabet

import pandasdata = pandas.read_csv( 'nato_phonetic_alphabet.csv' )phonetic_dict = {key.letter:key.code for (row,key) in data.iterrows()}...

 
 
 

Comments


© 2024 by GifTED. Powered and secured by Wix

bottom of page