-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock_app.py
358 lines (301 loc) · 11.5 KB
/
clock_app.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
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
# This file is part of the Python Projects repository, which is licensed under the
# Apache License, Version 2.0. You may obtain a copy of the license at
#
# http://www.apache.org/licenses/LICENSE-2.0
"""
Clock App Program.
Input:
- None.
Output:
- Displays a clock app with time, stopwatch, and buttons for start/stop and reset.
Features:
1. Display time, stopwatch, and buttons for start/stop and reset.
2. Light and dark mode color schemes.
3. Click sound for buttons.
4. Alarm functionality with sound.
5. Set, snooze, and stop alarm features.
6. Lap time recording for the stopwatch.
7. Toggle between light and dark modes.
Usage:
- Run the script to launch the clock app with the specified features.
"""
import time
from kivy.app import App
from kivy.clock import Clock
from kivy.core.text import LabelBase
from kivy.core.window import Window
from kivy.utils import get_color_from_hex
from kivy.core.audio import SoundLoader
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.metrics import dp
from kivy.lang import Builder
from time import strftime
import os
# Define colors for light and dark mode
light_mode_colors = {
'background': '#FFFFFF',
'text': '#000000',
'button_normal': '#4CAF50',
'button_down': '#45A049',
}
dark_mode_colors = {
'background': '#121212',
'text': '#FFFFFF',
'button_normal': '#333333',
'button_down': '#585858',
}
class AlarmPopup(Popup):
def __init__(self, set_alarm_callback, **kwargs):
super(AlarmPopup, self).__init__(**kwargs)
self.set_alarm_callback = set_alarm_callback
self.title = 'Set Alarm'
self.content = self._build_content()
def _build_content(self):
layout = BoxLayout(
orientation='vertical',
spacing=dp(10),
padding=dp(10),
size_hint=(None, None),
size=(dp(350), dp(400)),
pos_hint={'center_x': 0.5, 'center_y': 0.8}
)
inner_layout = BoxLayout(
orientation='vertical',
spacing=dp(10),
size_hint_y=None,
height=dp(350),
pos_hint={'center_x': 1.0, 'center_y': 1.0}
)
alarm_label = Label(
text='Time for alarm:',
size_hint=(None, None),
height=dp(45),
font_size=dp(24),
pos_hint={'center_x': 1.0, 'center_y': 1.0}
)
inner_layout.add_widget(alarm_label)
self.hour_input = TextInput(
hint_text='Hour',
input_filter='int',
multiline=False,
size_hint_y=None,
height=dp(50),
pos_hint={'center_x': 1.0, 'center_y': 1.0}
)
self.minute_input = TextInput(
hint_text='Minute',
input_filter='int',
multiline=False,
size_hint_y=None,
height=dp(50),
pos_hint={'center_x': 1.0, 'center_y': 1.0}
)
inner_layout.add_widget(self.hour_input)
inner_layout.add_widget(self.minute_input)
set_alarm_button = Button(
text='Set Alarm',
on_press=self.set_alarm,
size_hint_y=None,
height=dp(45),
font_size=dp(18),
pos_hint={'center_x': 1.0, 'center_y': 1.0}
)
inner_layout.add_widget(set_alarm_button)
layout.add_widget(inner_layout)
return layout
def set_alarm(self, instance):
try:
hour = int(self.hour_input.text)
minute = int(self.minute_input.text)
if 0 <= hour <= 23 and 0 <= minute <= 59:
self.set_alarm_callback(hour, minute)
self.dismiss()
else:
Popup(title='Invalid Input', content=Label(text='Please enter valid hour (0-23) and minute (0-59).'),
size_hint=(None, None), size=(dp(400), dp(200))).open()
except ValueError:
Popup(title='Invalid Input', content=Label(text='Please enter valid numerical values for hour and minute.'),
size_hint=(None, None), size=(dp(400), dp(200))).open()
class ClockApp(App):
sw_started = False
sw_seconds = 0
lap_times = ListProperty([])
dark_mode = True
def __init__(self, **kwargs):
super(ClockApp, self).__init__(**kwargs)
self.audio_folder = '../../assets/audio'
self.image_folder = '../../assets/images/image'
self.click_sound = SoundLoader.load(os.path.join(self.audio_folder, 'click_clockapp.wav'))
self.alarm_sound = None
self.alarm_hour = 0
self.alarm_minute = 0
self.alarm_triggered = False
def play_click_sound(self):
if self.click_sound:
self.click_sound.play()
def play_alarm_sound(self):
if self.alarm_sound:
self.alarm_sound.play()
def play_timer_expired_sound(self):
timer_expired_sound = SoundLoader.load(os.path.join(self.audio_folder, 'timer_expired_clockapp.wav'))
if timer_expired_sound:
timer_expired_sound.play()
def update_time(self, nap):
try:
if self.sw_started:
self.sw_seconds += nap
self.check_alarm_trigger()
minutes, seconds = divmod(self.sw_seconds, 60)
milliseconds = int(seconds * 100 % 100)
if self.root:
self.root.ids.stopwatch.text = (
'[size=40]%02d[/size]:[size=40]%02d[/size].[size=20]%02d[/size]' %
(int(minutes), int(seconds), milliseconds)
)
self.root.ids.time.text = strftime('[b]%H[/b]:%M:%S')
except Exception as e:
print(f"Error updating time: {e}")
def on_start(self):
Clock.schedule_interval(self.update_time, 0)
def start_stop(self):
if self.root:
self.root.ids.start_stop.text = (
'Start' if self.sw_started else 'Stop'
)
self.sw_started = not self.sw_started
self.play_click_sound()
def reset(self):
if self.sw_started:
if self.root:
self.root.ids.start_stop.text = 'Start'
self.sw_started = False
self.lap_times = []
self.sw_seconds = 0
self.play_click_sound()
def mark_lap(self):
if self.sw_started:
minutes, seconds = divmod(self.sw_seconds, 60)
milliseconds = int(seconds * 100 % 100)
lap_time = f'{int(minutes):02d}:{int(seconds):02d}.{milliseconds:02d}'
self.lap_times.insert(0, lap_time) # Insert lap time at the beginning of the list
if self.root:
self.root.ids.lap_times.text = '\n'.join(self.lap_times)
self.play_click_sound()
def set_alarm(self, hour, minute):
try:
print(f'Setting alarm for {hour:02d}:{minute:02d}')
self.alarm_hour = hour
self.alarm_minute = minute
self.alarm_sound = SoundLoader.load(os.path.join(self.audio_folder, 'alarm_clockapp.wav'))
print(f"Alarm set: {self.alarm_hour:02d}:{self.alarm_minute:02d}")
except Exception as e:
print(f"Error setting alarm: {e}")
def check_alarm_trigger(self):
if not self.alarm_triggered:
# Get the current time
current_time = time.localtime()
current_hour = current_time.tm_hour
current_minute = current_time.tm_min
current_second = current_time.tm_sec
if self.alarm_hour == current_hour and self.alarm_minute == current_minute and current_second == 0:
print("Alarm triggered!")
self.play_alarm_sound()
self.alarm_triggered = True
self.show_alarm_popup()
def snooze_alarm(self):
self.alarm_triggered = False
self.reset()
self.play_alarm_sound()
def stop_alarm(self):
self.alarm_triggered = False
if self.alarm_sound:
self.alarm_sound.stop()
def show_alarm_settings_popup(self):
alarm_popup = AlarmPopup(self.set_alarm)
alarm_popup.open()
def show_alarm_popup(self):
alarm_popup = Popup(title='Alarm!', content=self._build_alarm_popup_content(),
size_hint=(None, None), size=(dp(400), dp(200)))
alarm_popup.open()
def _build_alarm_popup_content(self):
layout = BoxLayout(
orientation='vertical',
spacing=dp(10),
padding=dp(10),
size_hint=(None, None),
size=(dp(300), dp(150)) # Adjust the size of the Popup
)
alarm_label = Label(
text='Alarm Time Reached!',
size_hint=(None, None),
height=dp(45),
font_size=dp(18) # Adjust the font size of the Label
)
layout.add_widget(alarm_label)
snooze_button = Button(
text='Snooze',
on_press=lambda instance: self.snooze_alarm(),
size_hint_y=None,
height=dp(40),
font_size=dp(16),
accessibility_hint='Snooze the alarm'
)
stop_button = Button(
text='Stop',
on_press=lambda instance: self.stop_alarm(),
size_hint_y=None,
height=dp(40),
font_size=dp(16) # Adjust the font size of the Button
)
button_layout = BoxLayout(
spacing=dp(10),
size_hint_y=None,
height=dp(80)
)
button_layout.add_widget(snooze_button)
button_layout.add_widget(stop_button)
layout.add_widget(button_layout)
return layout
def build(self):
self.apply_color_scheme(self.dark_mode)
return Builder.load_file('clock.kv')
def apply_color_scheme(self, dark_mode=False):
colors = dark_mode_colors if dark_mode else light_mode_colors
# Define the directory path where your font files are located
fonts_directory = os.path.abspath('../../assets/fonts')
# Define the font file names
regular_font = 'Roboto-Thin.ttf' if dark_mode else 'Roboto-Regular.ttf'
bold_font = 'Roboto-Medium.ttf'
# Construct the full file paths
regular_font_path = os.path.join(fonts_directory, regular_font)
bold_font_path = os.path.join(fonts_directory, bold_font)
LabelBase.register(name='Roboto',
fn_regular=regular_font_path,
fn_bold=bold_font_path)
# Set text color for specific labels
text_labels = ['time', 'stopwatch', 'lap_times']
for label_id in text_labels:
label = self.root.ids[label_id] # type: ignore
if label:
label.color = get_color_from_hex(colors['text'])
# Set text color for buttons
button_ids = ['start_stop', 'reset', 'toggle_dark_mode']
for button_id in button_ids:
button = self.root.ids[button_id] # type: ignore
if button:
button.color = get_color_from_hex(colors['text'])
if hasattr(self, 'alarm_popup') and self.alarm_popup:
self.alarm_popup.content.apply_color_scheme(dark_mode)
def toggle_dark_mode(self):
try:
self.dark_mode = not self.dark_mode
self.apply_color_scheme(self.dark_mode)
except Exception as e:
print(f"Error toggling dark mode: {e}")
if __name__ == '__main__':
ClockApp().run()