|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import ttk |
| 3 | +import math |
| 4 | + |
| 5 | +# Constants |
| 6 | +GREEN = '#90A17D' |
| 7 | +FONT_NAME = 'Courier' |
| 8 | +WORK_MIN = 25 |
| 9 | +SHORT_BREAK_MIN = 5 |
| 10 | +LONG_BREAK_MIN = 15 |
| 11 | +BG_COLOR = '#FFFDE7' |
| 12 | + |
| 13 | +# Global Variables |
| 14 | +reps = 0 |
| 15 | +timer = None |
| 16 | + |
| 17 | +# Function to reset the timer |
| 18 | +def reset_timer(): |
| 19 | + global reps, timer |
| 20 | + |
| 21 | + root.after_cancel(timer) |
| 22 | + canvas.itemconfig(timer_text, text='00:00', fill=BG_COLOR) # Set text color to the background color |
| 23 | + title_label.config(text='Timer', background=BG_COLOR, foreground='white', font=(FONT_NAME, 40, 'bold')) |
| 24 | + reps = 0 |
| 25 | + start_btn['state'] = tk.NORMAL |
| 26 | + |
| 27 | +# Function to start the timer |
| 28 | +def start_timer(): |
| 29 | + global reps, timer |
| 30 | + start_btn['state'] = tk.DISABLED |
| 31 | + |
| 32 | + reps += 1 |
| 33 | + |
| 34 | + if reps % 2 == 0: |
| 35 | + if reps == 8: |
| 36 | + long_break_sec = LONG_BREAK_MIN * 60 |
| 37 | + title_label.config(text='Long Break', background=BG_COLOR, foreground='white', font=(FONT_NAME, 40, 'bold')) |
| 38 | + count_down(long_break_sec) |
| 39 | + reps = 0 |
| 40 | + else: |
| 41 | + short_break_sec = SHORT_BREAK_MIN * 60 |
| 42 | + title_label.config(text='Short Break', background=BG_COLOR, foreground='white', font=(FONT_NAME, 40, 'bold')) |
| 43 | + count_down(short_break_sec) |
| 44 | + else: |
| 45 | + title_label.config(text='Work!', background=BG_COLOR, foreground=GREEN, font=(FONT_NAME, 40, 'bold')) # Set foreground color to green for "Work" |
| 46 | + work_sec = WORK_MIN * 60 |
| 47 | + count_down(work_sec) |
| 48 | + |
| 49 | +# Function for countdown |
| 50 | +def count_down(count): |
| 51 | + global timer |
| 52 | + count_min = math.floor(count/60) |
| 53 | + count_sec = count % 60 |
| 54 | + |
| 55 | + if count_sec < 10: |
| 56 | + count_sec = f'0{count_sec}' |
| 57 | + |
| 58 | + canvas.itemconfig(timer_text, text=f'{count_min}:{count_sec}', fill=BG_COLOR) # Set text color to the background color |
| 59 | + if count > 0: |
| 60 | + timer = root.after(1000, count_down, count-1) |
| 61 | + else: |
| 62 | + start_timer() |
| 63 | + |
| 64 | +# UI Setup |
| 65 | +root = tk.Tk() |
| 66 | +root.title('POMODORO APP') |
| 67 | +root.geometry('780x700') |
| 68 | +root.resizable(False, False) |
| 69 | +root.config(background=BG_COLOR, padx=45, pady=50) |
| 70 | + |
| 71 | +# Icon and Canvas Background |
| 72 | +image_icon = tk.PhotoImage(file='tomato.png') |
| 73 | +root.iconphoto(False, image_icon) |
| 74 | + |
| 75 | +# Labels |
| 76 | +title_label = ttk.Label(text='Timer', background=BG_COLOR, foreground='white', font=(FONT_NAME, 40, 'bold')) |
| 77 | +title_label.grid(column=1, row=0) |
| 78 | + |
| 79 | +# Canvas setup |
| 80 | +canvas = tk.Canvas(width=512, height=512, bg=BG_COLOR, highlightthickness=0) |
| 81 | +canvas.create_image(256, 256, image=image_icon) # Use the same image for canvas background |
| 82 | +timer_text = canvas.create_text(256, 310, text='00:00', fill=BG_COLOR, font=(FONT_NAME, 50, 'bold')) # Set text color to the background color |
| 83 | +canvas.grid(column=1, row=1) |
| 84 | + |
| 85 | +# Buttons |
| 86 | +start_btn = ttk.Button(text='Start', command=start_timer) |
| 87 | +start_btn.grid(column=0, row=2) |
| 88 | + |
| 89 | +reset_btn = ttk.Button(text='Reset', command=reset_timer) |
| 90 | +reset_btn.grid(column=2, row=2) |
| 91 | + |
| 92 | +root.mainloop() |
0 commit comments