Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 8089f1b

Browse files
committed
enhanced-clock-display
1 parent 7a8e812 commit 8089f1b

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

projects/Alarm Clock/clock.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
1-
# Importing the necessary modules
21
import tkinter
32
from time import strftime
43

54
# Creating the main application window
65
top = tkinter.Tk()
7-
top.title("Clock") # Setting the title of the window
6+
top.title("Dynamic Clock") # Updated title
87
top.resizable(0, 0) # Making the window non-resizable
98

9+
# Function to determine the time of day
10+
def get_time_of_day(hour):
11+
if 5 <= hour < 12:
12+
return "Morning"
13+
elif 12 <= hour < 18:
14+
return "Afternoon"
15+
else:
16+
return "Evening"
1017

1118
# Function to update the time display
12-
def time():
13-
# Get the current time in the format HH:MM:SS AM/PM
14-
string = strftime("%H:%M:%S %p")
19+
def update_time():
20+
current_time = strftime("%H:%M:%S %p")
21+
hour = int(strftime("%H"))
22+
time_of_day = get_time_of_day(hour)
1523

16-
# Update the text of the clockTime Label with the current time
17-
clockTime.config(text=string)
24+
# Update the text of the clockTime Label with the current time and time of day
25+
clock_time.config(text=current_time + f"\nGood {time_of_day}!")
1826

19-
# Schedule the time function to be called again after 1000 milliseconds (1 second)
20-
clockTime.after(1000, time)
27+
# Dynamically change the background color based on time of day
28+
color = "lightblue" if time_of_day == "Morning" else "lightyellow" if time_of_day == "Afternoon" else "lightcoral"
29+
top.configure(background=color)
2130

31+
# Schedule the update_time function to be called again after 1000 milliseconds (1 second)
32+
clock_time.after(1000, update_time)
2233

2334
# Creating a Label widget to display the time
24-
clockTime = tkinter.Label(
35+
clock_time = tkinter.Label(
2536
top,
2637
font=("courier new", 40),
2738
background="black",
2839
foreground="white",
2940
)
3041

3142
# Position the Label widget in the center of the window
32-
clockTime.pack(anchor="center")
43+
clock_time.pack(anchor="center")
3344

34-
# Call the time function to start updating the time display
35-
time()
45+
# Call the update_time function to start updating the time display
46+
update_time()
3647

3748
# Start the Tkinter main event loop
3849
top.mainloop()

0 commit comments

Comments
 (0)