1- from tkinter import Tk , Label
1+ from tkinter import Tk , Label , Entry , Button , messagebox
22from tkinter .font import Font
33import time
4+ from tkinter import Checkbutton , BooleanVar
5+ from tkinter import Canvas
6+
47
58
69class DigitalClock :
710 def __init__ (self , font = None ):
811 """Initialize the digital clock."""
912 self .create_window ()
1013 self .configure_window ()
14+ self .is_dark = True
1115 self .set_font (font )
1216 self .add_header ()
1317 self .add_clock ()
1418 self .add_date () # ✅ Added new method to show date
19+ self .add_theme_button ()
20+ # self.add_theme_toggle()
21+ self .is_24_hour = True
22+ self .alarm_time = None
23+ self .add_format_toggle ()
24+ self .add_alarm_section ()
1525 self .update_time_on_clock ()
1626
27+ def add_format_toggle (self ):
28+ self .format_button = Button (self .window ,
29+ text = "Switch to 12 Hour" ,
30+ font = ("times" , 15 , "bold" ),
31+ command = self .toggle_format )
32+ self .format_button .grid (row = 5 , column = 2 , pady = 5 )
33+
34+ def toggle_format (self ):
35+ if self .is_24_hour :
36+ self .is_24_hour = False
37+ self .format_button .config (text = "Switch to 24 Hour" )
38+ else :
39+ self .is_24_hour = True
40+ self .format_button .config (text = "Switch to 12 Hour" )
41+
1742 def create_window (self ):
1843 """Create the main window."""
1944 self .window = Tk ()
@@ -38,14 +63,67 @@ def add_clock(self):
3863 """Add the clock label to the window."""
3964 self .clock = Label (self .window , font = (
4065 'times' , 90 , 'bold' ), bg = 'blue' , fg = 'white' )
41- self .clock .grid (row = 2 , column = 2 , padx = 620 , pady = 250 )
66+ self .clock .grid (row = 2 , column = 2 , padx = 500 , pady = 100 )
67+ # self.clock.grid(row=2, column=2, pady=20)
4268
4369 def add_date (self ):
4470 """Add a date label below the clock."""
4571 self .date_label = Label (self .window , font = ('times' , 40 , 'bold' ), bg = 'black' , fg = 'white' )
4672 self .date_label .grid (row = 3 , column = 2 )
4773 self .update_date_on_clock ()
4874
75+ # def toggle_theme(self):
76+ # if self.is_dark:
77+ # self.window.config(bg="white")
78+ # self.clock.config(bg="white", fg="black")
79+ # self.date_label.config(bg="white", fg="black")
80+ # self.header.config(bg="lightgray", fg="black")
81+ # else:
82+ # self.window.config(bg="black")
83+ # self.clock.config(bg="black", fg="white")
84+ # self.date_label.config(bg="black", fg="white")
85+ # self.header.config(bg="gray", fg="white")
86+
87+ # self.is_dark = not self.is_dark
88+
89+ def toggle_theme (self ):
90+ if self .theme_var .get (): # Dark mode ON
91+ self .window .config (bg = "black" )
92+ self .clock .config (bg = "black" , fg = "white" )
93+ self .date_label .config (bg = "black" , fg = "white" )
94+ self .header .config (bg = "gray" , fg = "white" )
95+ self .theme_toggle .config (bg = "black" , fg = "white" )
96+ else : # Light mode
97+ self .window .config (bg = "white" )
98+ self .clock .config (bg = "white" , fg = "black" )
99+ self .date_label .config (bg = "white" , fg = "black" )
100+ self .header .config (bg = "lightgray" , fg = "black" )
101+ self .theme_toggle .config (bg = "white" , fg = "black" )
102+
103+ # def add_theme_button(self):
104+ # self.theme_button = Label(self.window, text="Toggle Theme",
105+ # font=("times", 20, "bold"),
106+ # bg="green", fg="white",
107+ # cursor="hand2")
108+ # self.theme_button.grid(row=4, column=2)
109+ # self.theme_button.bind("<Button-1>", lambda e: self.toggle_theme())
110+
111+ def add_theme_button (self ):
112+ self .theme_var = BooleanVar (value = True )
113+
114+ self .theme_toggle = Checkbutton (
115+ self .window ,
116+ text = "Dark Mode" ,
117+ variable = self .theme_var ,
118+ command = self .toggle_theme ,
119+ bg = "black" ,
120+ fg = "white" ,
121+ selectcolor = "black" ,
122+ font = ("times" , 15 , "bold" )
123+ )
124+
125+ self .theme_toggle .grid (row = 4 , column = 2 , pady = 10 )
126+
49127 def update_date_on_clock (self ):
50128 """Update the date displayed below the clock."""
51129 currentDate = time .strftime ("%d-%b-%Y" )
@@ -55,15 +133,47 @@ def update_date_on_clock(self):
55133
56134 def update_time_on_clock (self ):
57135 """Update the time displayed on the clock every second."""
58- currentTime = time .strftime ("%H:%M:%S" )
136+ # currentTime = time.strftime("%H:%M:%S")
137+
138+ if self .is_24_hour :
139+ currentTime = time .strftime ("%H:%M:%S" )
140+ else :
141+ currentTime = time .strftime ("%I:%M:%S %p" )
142+
143+ if self .alarm_time == currentTime :
144+ messagebox .showinfo ("Alarm" , "⏰ Time's Up!" )
145+ self .alarm_time = None
146+
59147 self .clock .config (text = currentTime )
60148 self .clock .after (1000 , self .update_time_on_clock )
61149
62150 def start (self ):
63151 """Start the Tkinter main loop."""
64152 self .window .mainloop ()
153+
154+ def add_alarm_section (self ):
155+ self .alarm_entry = Entry (self .window , font = ("times" , 15 ))
156+ self .alarm_entry .grid (row = 6 , column = 2 , pady = 5 )
157+
158+ self .alarm_button = Button (self .window ,
159+ text = "Set Alarm (HH:MM:SS)" ,
160+ font = ("times" , 12 , "bold" ),
161+ command = self .set_alarm )
162+ self .alarm_button .grid (row = 7 , column = 2 , pady = 5 )
163+
164+ def set_alarm (self ):
165+ alarm_input = self .alarm_entry .get ()
166+
167+ try :
168+ time .strptime (alarm_input , "%H:%M:%S" )
169+ self .alarm_time = alarm_input
170+ messagebox .showinfo ("Alarm Set" , f"Alarm set for { alarm_input } " )
171+ except ValueError :
172+ messagebox .showerror ("Invalid Format" , "Use HH:MM:SS format" )
173+
174+
65175
66176
67177if __name__ == "__main__" :
68178 clock = DigitalClock ()
69- clock .start ()
179+ clock .start ()
0 commit comments