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 diff --git a/CountDown Timer/main.py b/CountDown Timer/main.py index 83d1485..edaf802 100644 --- a/CountDown Timer/main.py +++ b/CountDown Timer/main.py @@ -2,98 +2,205 @@ import tkinter as tk class Application(Frame): - def __init__(self,master): - super(Application,self).__init__(master) + def __init__(self, master): + super(Application, self).__init__(master) self.pack() self.createWidgets() - self.master.geometry("300x200") + self.master.geometry("400x350") + self.master.configure(bg='#2c3e50') + self.master.resizable(False, False) + self._alarm_id = None self._paused = False self._starttime = 0 + self._remaining = 0 + self._is_running = False - # def startTimer(self): - def createWidgets(self): - - self.someFrame = Frame(self) - self.time_var = StringVar() - self.input_time = tk.Entry(self, width=25, font=('Calibri',12), textvariable = self.time_var) - # Adding placeholder - self.input_time.insert(0, "Enter Time in seconds") - self.input_time.pack(padx=10,pady=(30,10)) - #using bind method - self.input_time.bind("", self.enter) - - self.startButton = Button(self.someFrame, text="Start", font=('Helvetica',12), bg='green', fg='white', command=self.startTime) + # Title + title = Label(self, text="Countdown Timer", font=("Helvetica", 20, "bold"), + bg='#2c3e50', fg='white') + title.pack(pady=10) + + # Time display (MM:SS format) + self.labelvariable = StringVar() + self.labelvariable.set("00:00") + self.thelabel = Label(self, textvariable=self.labelvariable, + font=('Helvetica', 60, 'bold'), + bg='#2c3e50', fg='#2ecc71') + self.thelabel.pack(pady=20) + + # Input frame + input_frame = Frame(self, bg='#2c3e50') + input_frame.pack(pady=10) + + Label(input_frame, text="Enter seconds:", font=('Helvetica', 12), + bg='#2c3e50', fg='white').pack(side=LEFT, padx=5) + + self.input_time = tk.Entry(input_frame, width=10, font=('Calibri', 14)) + self.input_time.insert(0, "60") + self.input_time.pack(side=LEFT, padx=5) + self.input_time.bind("", lambda e: self.startTime()) + + # Preset buttons (10s, 30s, 60s) + preset_frame = Frame(self, bg='#2c3e50') + preset_frame.pack(pady=5) + + Label(preset_frame, text="Presets:", font=('Helvetica', 10), + bg='#2c3e50', fg='white').pack(side=LEFT, padx=5) + + Button(preset_frame, text="10s", font=('Helvetica', 10), + command=lambda: self.set_preset(10), + bg='#3498db', fg='white', width=5).pack(side=LEFT, padx=2) + + Button(preset_frame, text="30s", font=('Helvetica', 10), + command=lambda: self.set_preset(30), + bg='#3498db', fg='white', width=5).pack(side=LEFT, padx=2) + + Button(preset_frame, text="60s", font=('Helvetica', 10), + command=lambda: self.set_preset(60), + bg='#3498db', fg='white', width=5).pack(side=LEFT, padx=2) + + # Button frame + self.someFrame = Frame(self, bg='#2c3e50') + self.someFrame.pack(pady=15) + + self.startButton = Button(self.someFrame, text="Start", font=('Helvetica', 12), + bg='#2ecc71', fg='white', command=self.startTime, width=10) self.startButton.pack(side=LEFT, padx=5) - - self.pauseButton = Button(self.someFrame, text="Pause", font=('Helvetica',12), bg='azure', command=self.pauseTime) + + self.pauseButton = Button(self.someFrame, text="Pause", font=('Helvetica', 12), + bg='#f1c40f', fg='white', command=self.togglePause, width=10) self.pauseButton.pack(side=LEFT, padx=5) - - self.resetButton = Button(self.someFrame, text="Reset", font=('Helvetica',12), bg='azure', command=self.resetTime) + + self.resetButton = Button(self.someFrame, text="Reset", font=('Helvetica', 12), + bg='#e74c3c', fg='white', command=self.resetTime, width=10) self.resetButton.pack(side=LEFT, padx=5) - - self.closeButton = Button(self.someFrame, text="Close", font=('Helvetica',12), bg='red',fg='white', command=self.closeApp) + + self.closeButton = Button(self.someFrame, text="Close", font=('Helvetica', 12), + bg='#95a5a6', fg='white', command=self.closeApp, width=10) self.closeButton.pack(side=LEFT, padx=5) - self.someFrame.pack(side=TOP) - - self.labelvariable = StringVar() - self.labelvariable.set("") - - self.thelabel = Label(self,textvariable = self.labelvariable,font=('Helvetica',50)) - self.thelabel.pack(side=TOP) - - # Removes placeholder text in the Entry widget - def enter(self,*args): - self.input_time.delete(0, 'end') - # Resume function + # Progress bar + self.progress_canvas = Canvas(self, width=300, height=20, bg='#34495e', highlightthickness=0) + self.progress_canvas.pack(pady=15) + self.progress_bar = self.progress_canvas.create_rectangle(0, 0, 0, 20, fill='#2ecc71') + + def set_preset(self, seconds): + """Set preset time""" + self.input_time.delete(0, END) + self.input_time.insert(0, str(seconds)) + + def update_progress(self, current, total): + """Update the progress bar""" + if total > 0: + percentage = (current / total) * 300 + self.progress_canvas.coords(self.progress_bar, 0, 0, percentage, 20) + + # Change color based on remaining time + if current / total < 0.2: + self.progress_canvas.itemconfig(self.progress_bar, fill='#e74c3c') # Red + elif current / total < 0.5: + self.progress_canvas.itemconfig(self.progress_bar, fill='#f1c40f') # Yellow + else: + self.progress_canvas.itemconfig(self.progress_bar, fill='#2ecc71') # Green + + def format_time(self, seconds): + """Convert seconds to MM:SS format""" + minutes = seconds // 60 + seconds = seconds % 60 + return f"{minutes:02d}:{seconds:02d}" + + def togglePause(self): + """Toggle between Pause and Resume""" + if self._paused: + # Currently paused, so resume + self.resumeTime() + else: + # Currently running, so pause + self.pauseTime() + + def pauseTime(self): + """Pause the timer""" + if self._is_running and not self._paused: + self._paused = True + self.pauseButton.config(text="Resume", bg='#e67e22') + + def resumeTime(self): + """Resume the timer""" + if self._is_running and self._paused: + self._paused = False + self.pauseButton.config(text="Pause", bg='#f1c40f') + # The countdown loop will automatically continue + # because it checks self._paused in the loop + def startTime(self): - self.get_time = self.time_var.get() + """Start the timer""" + if self._is_running: + return + try: - self.time_in_int = int(self.get_time) - except: - self.time_in_int = 0 - self._starttime = self.time_in_int + self._starttime = int(self.input_time.get()) + if self._starttime <= 0: + return + except ValueError: + return + + self._remaining = self._starttime + self._is_running = True self._paused = False - if self._alarm_id is None: - self.countdown(self._starttime) - - # Pause function - def pauseTime(self): - if self._alarm_id is not None: - self._paused = True - - # Reset function + self.startButton.config(text="Running", bg='#27ae60') + self.pauseButton.config(text="Pause", bg='#f1c40f') + self.countdown(self._remaining) + def resetTime(self): + """Reset the timer to zero""" if self._alarm_id is not None: self.master.after_cancel(self._alarm_id) self._alarm_id = None - self._paused = False - self.countdown(0) - self._paused = True + + self._is_running = False + self._paused = False + self._remaining = 0 + self.labelvariable.set("00:00") + self.progress_canvas.coords(self.progress_bar, 0, 0, 0, 20) + self.progress_canvas.itemconfig(self.progress_bar, fill='#2ecc71') + self.startButton.config(text="Start", bg='#2ecc71') + self.pauseButton.config(text="Pause", bg='#f1c40f') def closeApp(self): + """Close the application""" + if self._alarm_id is not None: + self.master.after_cancel(self._alarm_id) self.master.destroy() - - def countdown(self, timeInSeconds, start=True): - if timeInSeconds == 0: - self._starttime=0 - self.labelvariable.set("0") + + def countdown(self, timeInSeconds): + """Countdown timer logic""" + if timeInSeconds <= 0: + self.labelvariable.set("00:00") + self._is_running = False + self.startButton.config(text="Start", bg='#2ecc71') + self.pauseButton.config(text="Pause", bg='#f1c40f') + self.progress_canvas.coords(self.progress_bar, 0, 0, 0, 20) return - if start: - self._starttime = timeInSeconds + if self._paused: - self._alarm_id = self.master.after(1000, self.countdown, timeInSeconds, False) - else: - app.labelvariable.set(timeInSeconds) - self._alarm_id = self.master.after(1000, self.countdown, timeInSeconds-1, False) - + # If paused, check again in 500ms + self._alarm_id = self.master.after(500, self.countdown, timeInSeconds) + return + + # Update display + self._remaining = timeInSeconds + self.labelvariable.set(self.format_time(timeInSeconds)) + self.update_progress(timeInSeconds, self._starttime) + + # Continue countdown + self._alarm_id = self.master.after(1000, self.countdown, timeInSeconds - 1) if __name__ == '__main__': root = Tk() root.title("Countdown Timer") + root.configure(bg='#2c3e50') app = Application(root) - root.mainloop() - + root.mainloop() \ No newline at end of file