1
+ import time
2
+ from tkinter import *
3
+
4
+ # ---------------------------- CONSTANTS ------------------------------- #
5
+ LIGHTGREEN = "#bedbbb"
6
+ GREEN = "#16c79a"
7
+ DARKGREEN = "#99d22d"
8
+ BLUE = "#dff3e3"
9
+ PINK = "#e2979c"
10
+ RED = "#e7305b"
11
+ FONT_NAME = "Courier"
12
+ WORK_MIN = 25
13
+ SHORT_BREAK_MIN = 5
14
+ LONG_BREAK_MIN = 20
15
+ CHECKMARK = '✓'
16
+ reps = 0
17
+ timer = None
18
+
19
+ # ---------------------------- TIMER RESET ------------------------------- #
20
+ def resetTimer ():
21
+ global reps
22
+ reps = 0
23
+ window .after_cancel (timer )
24
+ label1 .config (text = 'Timer' , fg = DARKGREEN )
25
+ canvas .itemconfig (timer_text , text = "00:00" )
26
+ label2 .config (text = '' )
27
+
28
+ # ---------------------------- TIMER MECHANISM ------------------------------- #
29
+ def startTimer ():
30
+ global reps
31
+ reps += 1
32
+ work_sec = WORK_MIN * 60
33
+ short_break_sec = SHORT_BREAK_MIN * 60
34
+ long_break_sec = LONG_BREAK_MIN * 60
35
+ if reps % 8 == 0 :
36
+ label1 .config (text = 'Break' , fg = RED )
37
+ countdown (long_break_sec )
38
+ elif reps % 2 == 0 :
39
+ label1 .config (text = 'Break' , fg = PINK )
40
+ countdown (short_break_sec )
41
+ else :
42
+ label1 .config (text = 'Work' , fg = GREEN )
43
+ countdown (work_sec )
44
+
45
+ # ---------------------------- COUNTDOWN MECHANISM ------------------------------- #
46
+ def countdown (count ):
47
+ minutes = count // 60
48
+ seconds = count % 60
49
+ canvas .itemconfig (timer_text , text = f"{ minutes :02d} :{ seconds :02d} " )
50
+ if count > 0 :
51
+ global timer
52
+ timer = window .after (1000 , countdown , count - 1 )
53
+ else :
54
+ startTimer ()
55
+ label2 .config (text = CHECKMARK * (reps // 2 ))
56
+
57
+ # ---------------------------- UI SETUP ------------------------------- #
58
+
59
+ window = Tk ()
60
+ window .title ('Pomodoro Timer App' )
61
+ window .minsize (width = 200 , height = 200 )
62
+ window .config (padx = 100 , pady = 50 , bg = BLUE )
63
+
64
+ # Canvas
65
+ background_img = PhotoImage (file = 'tomato.png' )
66
+ canvas = Canvas (width = 200 , height = 224 , bg = BLUE , highlightthickness = 0 )
67
+ canvas .create_image (100 , 112 , image = background_img )
68
+ timer_text = canvas .create_text (102 , 130 , text = '00:00' , fill = 'white' , font = (FONT_NAME ,35 ,'bold' ))
69
+ canvas .grid (row = 2 ,column = 2 )
70
+
71
+ # Label
72
+ label1 = Label (text = 'Timer' , font = (FONT_NAME ,40 ), bg = BLUE , fg = DARKGREEN )
73
+ label1 .grid (row = 1 , column = 2 )
74
+ label2 = Label (text = '' , font = (FONT_NAME ,14 ,'normal' ), bg = BLUE , fg = GREEN )
75
+ label2 .grid (row = 4 , column = 2 )
76
+
77
+ def start ():
78
+ startTimer ()
79
+
80
+ def reset ():
81
+ resetTimer ()
82
+
83
+ # Button
84
+ start_btn = Button (text = 'Start' , command = start , font = FONT_NAME , bg = LIGHTGREEN , highlightthickness = 0 )
85
+ start_btn .grid (row = 3 , column = 1 )
86
+ reset_btn = Button (text = 'Reset' , command = reset , font = FONT_NAME , bg = LIGHTGREEN , highlightthickness = 0 )
87
+ reset_btn .grid (row = 3 , column = 3 )
88
+
89
+ window .mainloop ()
0 commit comments