diff --git a/Alarm Bot/main.py b/Alarm Bot/main.py index 87a225d..4ae2a30 100644 --- a/Alarm Bot/main.py +++ b/Alarm Bot/main.py @@ -3,68 +3,112 @@ import time import winsound from threading import * +import os # Add this to check if sound file exists root = Tk() -root.geometry("400x200") +root.geometry("400x250") + +# Global variable to control alarm state +alarm_active = False + +def stop_alarm(): + """Stop the ringing alarm""" + global alarm_active + alarm_active = False + winsound.PlaySound(None, winsound.SND_ASYNC) + print("Alarm stopped!") + +def play_alarm_sound(): + """Play alarm sound or show visual alert if sound file missing""" + try: + # Check if sound.wav exists + if os.path.exists("sound.wav"): + winsound.PlaySound("sound.wav", winsound.SND_ASYNC) + else: + # If no sound file, print a visual alarm + print("ALARM! ALARM! ") + # Make the window flash or show a message + root.title("ALARM RINGING!") + # Beep as fallback (Windows only) + winsound.Beep(1000, 500) # 1000 Hz for 500ms + except Exception as e: + print(f"🔔ALARM RINGING! (Sound error: {e})") def Threading(): - t1=Thread(target=alarm) - t1.start() + """Start the alarm in a separate thread""" + global alarm_active + alarm_active = True + t1 = Thread(target=alarm) + t1.daemon = True + t1.start() def alarm(): - while True: - set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}" - time.sleep(1) - current_time = datetime.datetime.now().strftime("%H:%M:%S") - print(current_time,set_alarm_time) + """Check time and trigger alarm when it matches set time""" + global alarm_active + + while True: + set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}" + time.sleep(1) + current_time = datetime.datetime.now().strftime("%H:%M:%S") + print(f"⏰ Current: {current_time} | Set: {set_alarm_time}") + + if current_time == set_alarm_time: + print("IT'S TIME!") + + # Ring the alarm in a loop until stopped + ring_count = 0 + while alarm_active and ring_count < 10: # Ring 10 times + play_alarm_sound() + time.sleep(1) + ring_count += 1 + + # Reset alarm state + if alarm_active: + alarm_active = False + root.title("Alarm Clock") + print("Alarm finished (10 rings)") - if current_time == set_alarm_time: - print("Time to Wake up") - winsound.PlaySound("sound.wav",winsound.SND_ASYNC) +def update_title(): + """Update window title when alarm is ringing""" + if alarm_active: + root.title("ALARM RINGING!") + else: + root.title("Alarm Clock") -Label(root,text="Alarm Clock",font=("Helvetica 20 bold"),fg="red").pack(pady=10) -Label(root,text="Set Time",font=("Helvetica 15 bold")).pack() +# UI Setup +Label(root, text="Alarm Clock", font=("Helvetica 20 bold"), fg="red").pack(pady=10) +Label(root, text="Set Time", font=("Helvetica 15 bold")).pack() frame = Frame(root) frame.pack() +# Hour dropdown hour = StringVar(root) hours = ('00', '01', '02', '03', '04', '05', '06', '07', - '08', '09', '10', '11', '12', '13', '14', '15', - '16', '17', '18', '19', '20', '21', '22', '23', '24' - ) + '08', '09', '10', '11', '12', '13', '14', '15', + '16', '17', '18', '19', '20', '21', '22', '23', '24') hour.set(hours[0]) - hrs = OptionMenu(frame, hour, *hours) hrs.pack(side=LEFT) +# Minute dropdown minute = StringVar(root) -minutes = ('00', '01', '02', '03', '04', '05', '06', '07', - '08', '09', '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') +minutes = tuple(f"{i:02d}" for i in range(61)) minute.set(minutes[0]) - mins = OptionMenu(frame, minute, *minutes) mins.pack(side=LEFT) +# Second dropdown second = StringVar(root) -seconds = ('00', '01', '02', '03', '04', '05', '06', '07', - '08', '09', '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') +seconds = tuple(f"{i:02d}" for i in range(61)) second.set(seconds[0]) - secs = OptionMenu(frame, second, *seconds) secs.pack(side=LEFT) -Button(root,text="Set Alarm",font=("Helvetica 15"),command=Threading).pack(pady=20) -root.mainloop() +# Buttons +Button(root, text="Set Alarm", font=("Helvetica 15"), + command=Threading, bg="green", fg="white").pack(pady=10) +Button(root, text="Stop Alarm", font=("Helvetica 15"), + command=stop_alarm, bg="red", fg="white").pack(pady=5) + +root.mainloop() \ No newline at end of file