-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.py
More file actions
426 lines (370 loc) · 13.6 KB
/
timer.py
File metadata and controls
426 lines (370 loc) · 13.6 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import sys
import json
import asyncio
import customtkinter as ctk
import time
import re
from plyer import notification
from dateutil import parser
from datetime import datetime, timedelta
# Set theme and color scheme
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")
class ModernTimer:
"""
A modern countdown timer with GUI interface.
Features:
- Visual countdown with circular progress indicator
- Natural language time input parsing
- Modern dark theme interface
- System notifications when timer completes
"""
def __init__(self, initial_time=0):
# Initialize the main window
self.window = ctk.CTk()
self.window.title("Timer")
# Center the window on screen
window_width = 500
window_height = 600
screen_width = self.window.winfo_screenwidth()
screen_height = self.window.winfo_screenheight()
center_x = int(screen_width/2 - window_width/2)
center_y = int(screen_height/2 - window_height/2)
self.window.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
self.window.configure(fg_color="#1a1a1a")
# Initialize state variables
self.time_left = initial_time
self.running = False
self.original_time = initial_time
self.setup_gui()
# If initial time was provided, start the timer automatically
if initial_time > 0:
self.update_display()
self.reset_button.configure(state="normal")
self.start_pause()
def setup_gui(self):
"""Set up all GUI elements with a modern design"""
# Main container
container = ctk.CTkFrame(self.window, fg_color="#2d2d2d")
container.pack(pady=20, padx=20, fill="both", expand=True)
# Time input section at the top
self.setup_time_input(container)
# Timer display frame
timer_frame = ctk.CTkFrame(container, fg_color="#363636")
timer_frame.pack(pady=20, padx=20, fill="x")
# Create canvas for the timer ring
self.canvas = ctk.CTkCanvas(
timer_frame,
width=300,
height=300,
bg='#363636',
highlightthickness=0
)
self.canvas.pack(pady=20)
# Draw the background ring
self.canvas.create_oval(
25, 25, 275, 275,
width=8,
outline='#404040'
)
# Draw the progress ring (accent color)
self.progress_ring = self.canvas.create_arc(
25, 25, 275, 275,
start=90,
extent=360,
width=8,
outline='#9b59b6',
style="arc"
)
# Time display in the center of the ring
self.time_label = ctk.CTkLabel(
timer_frame,
text="00:00:00",
font=ctk.CTkFont(family="Arial", size=36, weight="bold"),
text_color="#ffffff"
)
self.time_label.place(relx=0.5, rely=0.5, anchor="center")
# Control buttons below the timer
self.setup_control_buttons(container)
def setup_time_input(self, parent):
"""Create a modern time input section"""
input_frame = ctk.CTkFrame(parent, fg_color="#363636")
input_frame.pack(pady=20, padx=20, fill="x")
# Create three input fields with labels
self.hours_var = ctk.StringVar(value="0")
self.minutes_var = ctk.StringVar(value="0")
self.seconds_var = ctk.StringVar(value="0")
time_inputs = [
("Hours", self.hours_var),
("Minutes", self.minutes_var),
("Seconds", self.seconds_var)
]
fields_frame = ctk.CTkFrame(input_frame, fg_color="transparent")
fields_frame.pack(pady=20, expand=True)
for col, (label, var) in enumerate(time_inputs):
container = ctk.CTkFrame(fields_frame, fg_color="transparent")
container.grid(row=0, column=col, padx=15)
entry = ctk.CTkEntry(
container,
width=70,
height=40,
textvariable=var,
font=ctk.CTkFont(size=16),
placeholder_text="00"
)
entry.grid(row=0, column=0, padx=5)
ctk.CTkLabel(
container,
text=label,
font=ctk.CTkFont(size=14)
).grid(row=1, column=0, pady=(5,0))
# Start button
ctk.CTkButton(
input_frame,
text="Set Timer",
command=self.set_timer,
font=ctk.CTkFont(size=14),
fg_color="#9b59b6",
hover_color="#8e44ad",
width=120,
height=40
).pack(pady=20)
def setup_control_buttons(self, parent):
"""Create control buttons with modern styling"""
button_frame = ctk.CTkFrame(parent, fg_color="transparent")
button_frame.pack(pady=20)
# Pause/Resume button
self.start_button = ctk.CTkButton(
button_frame,
text="⏸️ Pause",
command=self.start_pause,
font=ctk.CTkFont(size=14),
fg_color="#2ecc71",
hover_color="#27ae60",
width=120,
height=40
)
self.start_button.pack(side="left", padx=10)
# Reset button
self.reset_button = ctk.CTkButton(
button_frame,
text="🔄 Restart",
command=self.reset,
font=ctk.CTkFont(size=14),
fg_color="#e74c3c",
hover_color="#c0392b",
width=120,
height=40,
state="disabled"
)
self.reset_button.pack(side="left", padx=10)
def validate_number(self, value):
"""Validate input to ensure only numbers 0-99 are entered"""
if value == "":
return True
try:
num = int(value)
return 0 <= num <= 99
except ValueError:
return False
def set_timer(self):
"""Set the timer based on input values"""
try:
hours = int(self.hours_var.get() or 0)
minutes = int(self.minutes_var.get() or 0)
seconds = int(self.seconds_var.get() or 0)
if minutes >= 60 or seconds >= 60:
raise ValueError("Minutes and seconds must be less than 60")
total_seconds = hours * 3600 + minutes * 60 + seconds
if total_seconds == 0:
return
self.time_left = total_seconds
self.original_time = total_seconds
self.update_display()
self.reset_button.configure(state="normal")
self.start_pause() # Auto-start the timer
except ValueError as e:
print(f"Invalid input: {str(e)}")
def start_pause(self):
"""Toggle between running and paused states"""
if self.time_left == 0:
return
if not self.running:
self.running = True
self.start_button.configure(
text="⏸️ Pause",
fg_color="#f1c40f",
hover_color="#f39c12"
)
self.update()
else:
self.running = False
self.start_button.configure(
text="▶ Resume",
fg_color="#2ecc71",
hover_color="#27ae60"
)
def update(self):
"""Update the timer countdown"""
if self.running and self.time_left > 0:
self.time_left -= 1
self.update_display()
if self.time_left > 0:
self.window.after(1000, self.update)
else:
self.timer_complete()
def update_display(self):
"""Update the visual elements (time display and progress ring)"""
# Update time label
hours = self.time_left // 3600
minutes = (self.time_left % 3600) // 60
seconds = self.time_left % 60
time_str = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
self.time_label.configure(text=time_str)
# Update progress ring
if self.original_time > 0:
progress = self.time_left / self.original_time
degrees = progress * 360
self.canvas.itemconfig(self.progress_ring, extent=degrees)
def timer_complete(self):
"""Handle timer completion"""
self.running = False
self.start_button.configure(
text="▶ Start",
fg_color="#2ecc71",
hover_color="#27ae60"
)
self.time_label.configure(text="Time's Up!")
self.canvas.itemconfig(self.progress_ring, extent=0)
# Send system notification
notification.notify(
title='Timer Complete',
message='Your timer has finished!',
app_name='PythonTimer',
timeout=10
)
def reset(self):
"""Reset the timer to original time"""
self.running = False
self.time_left = self.original_time
self.start_button.configure(
text="▶ Start",
fg_color="#2ecc71",
hover_color="#27ae60"
)
self.update_display()
def run(self):
"""Start the timer application"""
self.window.mainloop()
def parse_time_str(time_str):
"""
Parse natural language time string into seconds.
Handles formats like:
- '5 minutes'
- '2h 30m'
- '1 hour 30 minutes'
- '90s'
- '1:30:00'
- '2.5 hours'
"""
try:
# Remove any leading/trailing whitespace
time_str = time_str.strip().lower()
# Try parsing HH:MM:SS format first
if ':' in time_str:
parts = time_str.split(':')
if len(parts) == 3:
h, m, s = map(int, parts)
return h * 3600 + m * 60 + s
elif len(parts) == 2:
m, s = map(int, parts)
return m * 60 + s
# Handle abbreviated formats like "2h30m", "5m30s"
abbreviated = re.match(r'^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$',
time_str.replace(' ', ''))
if abbreviated:
h, m, s = abbreviated.groups()
total = 0
if h: total += int(h) * 3600
if m: total += int(m) * 60
if s: total += int(s)
if total > 0:
return total
# Handle decimal hours/minutes (e.g., "2.5 hours", "1.5 minutes")
decimal = re.match(r'(\d*\.?\d+)\s*(hour|minute|second)s?', time_str)
if decimal:
value = float(decimal.group(1))
unit = decimal.group(2)
if unit == 'hour':
return int(value * 3600)
elif unit == 'minute':
return int(value * 60)
elif unit == 'second':
return int(value)
# Try parsing with dateutil for more natural language
try:
# Create a base time
base_time = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
# Parse the relative time
parsed_time = parser.parse(time_str, default=base_time, fuzzy=True)
# Calculate the difference
delta = parsed_time - base_time
return int(delta.total_seconds())
except:
pass
# Handle simple numeric input (assume minutes)
if time_str.isdigit():
return int(time_str) * 60
raise ValueError("Could not parse time string")
except Exception as e:
print(f"Error parsing time: {str(e)}")
return 0
async def func(args):
"""Handle timer commands for integration"""
try:
command = args.get('command', '').lower()
time_str = args.get('time', '')
if command == 'open':
# Handle "set timer for X minutes" type commands
if time_str:
seconds = parse_time_str(time_str)
if seconds > 0:
timer = ModernTimer(seconds)
timer.run()
return json.dumps({"message": f"Timer started for {time_str}"})
# Just open timer window without setting time
timer = ModernTimer()
timer.run()
return json.dumps({"message": "Timer opened"})
elif command == 'close':
# Close any existing timer windows
if ctk._CTk__cls_windows:
for window in ctk._CTk__cls_windows:
window.destroy()
return json.dumps({"message": "Timer closed"})
else:
return json.dumps({
"error": "Invalid command. Use 'open' or 'close'"
})
except Exception as e:
return json.dumps({"error": f"Error: {str(e)}"})
# Integration configuration
object = {
"name": "timer",
"description": "Set and control a countdown timer. Examples: 'Set timer for 5 minutes', 'Start 2 hour timer', 'Open timer', 'Close timer'",
"parameters": {
"type": "object",
"properties": {
"command": {
"type": "string",
"enum": ["open", "close"],
"description": "Open or close the timer"
},
"time": {
"type": "string",
"description": "Time duration in natural language (e.g., '5 minutes', '2 hours')"
}
},
"required": ["command"]
}
}