Skip to main content
Code Review

Return to Question

edited tags
Link
Reinderien
  • 70.9k
  • 5
  • 76
  • 256
Source Link

Pomodoro Work Timer with GUI

My first actual code using GUI. Based off a class I took. Code criticism much appreciated. (I was all over the place with this one)

Resources

Picture:

tomato.png

Sound: https://www.freesoundslibrary.com/success-sound-effect/

Put these in the same directory as main.py

main.py

from tkinter import *
import math
from pygame import mixer
mixer.init()
# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20
reps = 1
timer = None
mixer.music.load('success-sound-effect.mp3')
def focus_window():
 window.attributes('-topmost', 1)
 window.focus_force()
 window.attributes('-topmost', 0)
# ---------------------------- TIMER RESET ------------------------------- # 
def reset_timer():
 global timer
 global reps
 window.after_cancel(timer)
 checkmark.config(text='')
 timer_label.config(text='Timer', fg=GREEN)
 canvas.itemconfig(timer_text, text='00:00')
 start_button.config(state="normal")
 reps = 1
# ---------------------------- TIMER MECHANISM ------------------------------- # 
def start_timer():
 global reps
 start_button.config(state="disabled")
 work_sec = WORK_MIN * 60
 short_sec = SHORT_BREAK_MIN * 60
 long_sec = LONG_BREAK_MIN * 60
 if reps % 2 != 0:
 reps += 1
 countdown(work_sec)
 timer_label.config(text='Work', fg=GREEN)
 else:
 if reps == 8:
 reps = 1
 countdown(long_sec)
 timer_label.config(text='Break', fg=RED)
 else:
 reps += 1
 countdown(short_sec)
 timer_label.config(text='Break', fg=PINK)
# ---------------------------- COUNTDOWN MECHANISM ------------------------------- # 
def countdown(count):
 global reps
 count_min = math.floor(count/60)
 count_sec = count % 60
 canvas.itemconfig(timer_text, text=f'{count_min:02}:{count_sec:02}')
 if count > 0:
 global timer
 timer = window.after(1000, countdown, count - 1)
 else:
 if reps % 2 == 0: checkmark.config(text="✔" * int(reps / 2))
 mixer.music.play()
 focus_window()
 start_timer()
# ---------------------------- UI SETUP ------------------------------- #
window = Tk()
window.title('Work Timer')
window.config(padx=100, pady=50, bg=YELLOW)
window.resizable(width=False, height=False)
tomato_img = PhotoImage(file='tomato.png')
canvas = Canvas(width=200, height=224, bg=YELLOW, highlightthickness=0)
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)
timer_label = Label(text='Timer', bg=YELLOW, fg=GREEN, font=(FONT_NAME, 55, 'bold'))
timer_label.grid(column=1, row=0)
start_button = Button(text='Start', highlightthickness=0, command=start_timer)
start_button.grid(column=0,row=2)
reset_button = Button(text='Reset', highlightthickness=0, command=reset_timer)
reset_button.grid(column=2,row=2)
checkmark = Label(text='',fg=GREEN, bg=YELLOW, font=(FONT_NAME, 25, 'bold'))
checkmark.grid(column=1, row=3)
window.mainloop()
lang-py

AltStyle によって変換されたページ (->オリジナル) /