diff --git a/Digital_Clock/README.MD b/Digital_Clock/README.MD index 263da1f..683b357 100644 --- a/Digital_Clock/README.MD +++ b/Digital_Clock/README.MD @@ -34,4 +34,8 @@ A simple Python-based digital clock application with a graphical user interface 3. **Execution:** The application runs continuously, keeping the time accurate until the user closes the window. - +## Features Added +- Alarm functionality +- 12/24 hour toggle +- Theme switch +- Improved input validation \ No newline at end of file diff --git a/Digital_Clock/digital_clock.py b/Digital_Clock/digital_clock.py index 9d6fc29..d8775ee 100644 --- a/Digital_Clock/digital_clock.py +++ b/Digital_Clock/digital_clock.py @@ -1,6 +1,9 @@ -from tkinter import Tk, Label +from tkinter import Tk, Label, Entry, Button, messagebox from tkinter.font import Font import time +from tkinter import Checkbutton, BooleanVar +from tkinter import Canvas + class DigitalClock: @@ -8,12 +11,34 @@ def __init__(self, font=None): """Initialize the digital clock.""" self.create_window() self.configure_window() + self.is_dark = True self.set_font(font) self.add_header() self.add_clock() self.add_date() # ✅ Added new method to show date + self.add_theme_button() + # self.add_theme_toggle() + self.is_24_hour = True + self.alarm_time = None + self.add_format_toggle() + self.add_alarm_section() self.update_time_on_clock() + def add_format_toggle(self): + self.format_button = Button(self.window, + text="Switch to 12 Hour", + font=("times", 15, "bold"), + command=self.toggle_format) + self.format_button.grid(row=5, column=2, pady=5) + + def toggle_format(self): + if self.is_24_hour: + self.is_24_hour = False + self.format_button.config(text="Switch to 24 Hour") + else: + self.is_24_hour = True + self.format_button.config(text="Switch to 12 Hour") + def create_window(self): """Create the main window.""" self.window = Tk() @@ -38,7 +63,8 @@ def add_clock(self): """Add the clock label to the window.""" self.clock = Label(self.window, font=( 'times', 90, 'bold'), bg='blue', fg='white') - self.clock.grid(row=2, column=2, padx=620, pady=250) + self.clock.grid(row=2, column=2, padx=500, pady=100) + # self.clock.grid(row=2, column=2, pady=20) def add_date(self): """Add a date label below the clock.""" @@ -46,6 +72,58 @@ def add_date(self): self.date_label.grid(row=3, column=2) self.update_date_on_clock() + # def toggle_theme(self): + # if self.is_dark: + # self.window.config(bg="white") + # self.clock.config(bg="white", fg="black") + # self.date_label.config(bg="white", fg="black") + # self.header.config(bg="lightgray", fg="black") + # else: + # self.window.config(bg="black") + # self.clock.config(bg="black", fg="white") + # self.date_label.config(bg="black", fg="white") + # self.header.config(bg="gray", fg="white") + + # self.is_dark = not self.is_dark + + def toggle_theme(self): + if self.theme_var.get(): # Dark mode ON + self.window.config(bg="black") + self.clock.config(bg="black", fg="white") + self.date_label.config(bg="black", fg="white") + self.header.config(bg="gray", fg="white") + self.theme_toggle.config(bg="black", fg="white") + else: # Light mode + self.window.config(bg="white") + self.clock.config(bg="white", fg="black") + self.date_label.config(bg="white", fg="black") + self.header.config(bg="lightgray", fg="black") + self.theme_toggle.config(bg="white", fg="black") + + # def add_theme_button(self): + # self.theme_button = Label(self.window, text="Toggle Theme", + # font=("times", 20, "bold"), + # bg="green", fg="white", + # cursor="hand2") + # self.theme_button.grid(row=4, column=2) + # self.theme_button.bind("", lambda e: self.toggle_theme()) + + def add_theme_button(self): + self.theme_var = BooleanVar(value=True) + + self.theme_toggle = Checkbutton( + self.window, + text="Dark Mode", + variable=self.theme_var, + command=self.toggle_theme, + bg="black", + fg="white", + selectcolor="black", + font=("times", 15, "bold") + ) + + self.theme_toggle.grid(row=4, column=2, pady=10) + def update_date_on_clock(self): """Update the date displayed below the clock.""" currentDate = time.strftime("%d-%b-%Y") @@ -55,15 +133,47 @@ def update_date_on_clock(self): def update_time_on_clock(self): """Update the time displayed on the clock every second.""" - currentTime = time.strftime("%H:%M:%S") + # currentTime = time.strftime("%H:%M:%S") + + if self.is_24_hour: + currentTime = time.strftime("%H:%M:%S") + else: + currentTime = time.strftime("%I:%M:%S %p") + + if self.alarm_time == currentTime: + messagebox.showinfo("Alarm", "⏰ Time's Up!") + self.alarm_time = None + self.clock.config(text=currentTime) self.clock.after(1000, self.update_time_on_clock) def start(self): """Start the Tkinter main loop.""" self.window.mainloop() + + def add_alarm_section(self): + self.alarm_entry = Entry(self.window, font=("times", 15)) + self.alarm_entry.grid(row=6, column=2, pady=5) + + self.alarm_button = Button(self.window, + text="Set Alarm (HH:MM:SS)", + font=("times", 12, "bold"), + command=self.set_alarm) + self.alarm_button.grid(row=7, column=2, pady=5) + + def set_alarm(self): + alarm_input = self.alarm_entry.get() + + try: + time.strptime(alarm_input, "%H:%M:%S") + self.alarm_time = alarm_input + messagebox.showinfo("Alarm Set", f"Alarm set for {alarm_input}") + except ValueError: + messagebox.showerror("Invalid Format", "Use HH:MM:SS format") + + if __name__ == "__main__": clock = DigitalClock() - clock.start() + clock.start() \ No newline at end of file