-
Notifications
You must be signed in to change notification settings - Fork 0
/
pomodoro.py
147 lines (116 loc) · 4.58 KB
/
pomodoro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import sys
import tkinter as tk
from tkinter import ttk
import pygame
if getattr(sys, "frozen", False):
# If the application is frozen (e.g., PyInstaller), use this path
app_path = sys._MEIPASS
else:
# If running in a script, use the current directory
app_path = os.path.dirname(os.path.abspath(__file__))
# Set the icon for the application
icon_path = os.path.join(app_path, "timer.ico")
class PomodoroApp:
def __init__(self, root):
self.root = root
self.root.title("Pomodoro Timer")
# Double the width of the window
new_width = (
self.root.winfo_screenwidth() * 1 // 3
) # Set to 2/3 of the screen width
new_height = (
self.root.winfo_screenheight() * 1 // 2
) # Keep the height unchanged
self.root.geometry(f"{new_width}x{new_height}")
self.minutes = 25 # Initial time
self.seconds = 0
self.timer = None # Initialize the timer attribute
self.timer_label = ttk.Label(root, text="25:00", font=("Helvetica", 48))
self.timer_label.pack(pady=10)
# Titles for minute and second boxes
ttk.Label(root, text="Minutes:").pack()
self.time_list_minutes = ttk.Combobox(
root, values=[0, 5, 10, 15, 20, 25, 30, 45, 60], state="readonly"
)
self.time_list_minutes.set("25")
self.time_list_minutes.pack(pady=5)
ttk.Label(root, text="Seconds:").pack()
self.time_list_seconds = ttk.Combobox(
root, values=[0, 5, 10, 15, 20, 30], state="readonly"
)
self.time_list_seconds.set("0")
self.time_list_seconds.pack(pady=5)
self.start_button = ttk.Button(root, text="Start", command=self.start_timer)
self.start_button.pack(pady=5)
self.pause_button = ttk.Button(root, text="Pause", command=self.pause_timer)
self.pause_button.pack(pady=5)
self.stop_button = ttk.Button(root, text="Stop", command=self.stop_timer)
self.stop_button.pack(pady=5)
self.reset_button = ttk.Button(root, text="Reset", command=self.reset_timer)
self.reset_button.pack(pady=5)
self.paused = False
self.timer_running = False
# Create Stop Alarm button
self.stop_alarm_button = ttk.Button(
root,
text="Stop Alarm",
command=self.stop_alarm,
)
self.stop_alarm_button.pack(pady=5)
# Initialize pygame mixer for playing alarm sound
pygame.mixer.init()
# Check if the alarm sound file exists
alarm_sound_path = "alarm.mp3"
if not os.path.exists(alarm_sound_path):
print("Error: The 'alarm.mp3' file is missing.")
self.root.destroy()
return
self.alarm_sound_path = alarm_sound_path
def start_timer(self):
if not self.timer_running:
if not self.paused:
self.minutes = int(self.time_list_minutes.get())
self.seconds = int(self.time_list_seconds.get())
self.update_timer()
self.timer_running = True
def pause_timer(self):
if self.timer_running:
self.root.after_cancel(self.timer)
self.paused = True
self.timer_running = False
def stop_timer(self):
if self.timer_running:
self.root.after_cancel(self.timer)
self.paused = False
self.timer_label["text"] = f"{self.minutes:02d}:{self.seconds:02d}"
self.timer_running = False
def reset_timer(self):
if self.timer_running:
self.root.after_cancel(self.timer)
self.minutes = int(self.time_list_minutes.get())
self.seconds = int(self.time_list_seconds.get())
self.timer_label["text"] = f"{self.minutes:02d}:{self.seconds:02d}"
self.paused = False
self.timer_running = False
def update_timer(self):
self.timer_label["text"] = f"{self.minutes:02d}:{self.seconds:02d}"
if self.minutes == 0 and self.seconds == 0:
self.stop_timer()
self.play_alarm() # Play alarm sound when the timer reaches 0:00
return
self.timer = self.root.after(1000, self.update_timer)
if self.seconds == 0:
self.minutes -= 1
self.seconds = 59
else:
self.seconds -= 1
def play_alarm(self):
pygame.mixer.music.load(self.alarm_sound_path)
pygame.mixer.music.play(-1)
def stop_alarm(self):
pygame.mixer.music.stop()
if __name__ == "__main__":
root = tk.Tk()
app = PomodoroApp(root)
root.mainloop()