Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Digital_Clock/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
118 changes: 114 additions & 4 deletions Digital_Clock/digital_clock.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
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:
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()
Expand All @@ -38,14 +63,67 @@ 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."""
self.date_label = Label(self.window, font=('times', 40, 'bold'), bg='black', fg='white')
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("<Button-1>", 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")
Expand All @@ -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()