|
1 | | -# Importing the necessary modules |
2 | 1 | import tkinter |
3 | 2 | from time import strftime |
4 | 3 |
|
5 | 4 | # Creating the main application window |
6 | 5 | top = tkinter.Tk() |
7 | | -top.title("Clock") # Setting the title of the window |
| 6 | +top.title("Dynamic Clock") # Updated title |
8 | 7 | top.resizable(0, 0) # Making the window non-resizable |
9 | 8 |
|
| 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" |
10 | 17 |
|
11 | 18 | # 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) |
15 | 23 |
|
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}!") |
18 | 26 |
|
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) |
21 | 30 |
|
| 31 | + # Schedule the update_time function to be called again after 1000 milliseconds (1 second) |
| 32 | + clock_time.after(1000, update_time) |
22 | 33 |
|
23 | 34 | # Creating a Label widget to display the time |
24 | | -clockTime = tkinter.Label( |
| 35 | +clock_time = tkinter.Label( |
25 | 36 | top, |
26 | 37 | font=("courier new", 40), |
27 | 38 | background="black", |
28 | 39 | foreground="white", |
29 | 40 | ) |
30 | 41 |
|
31 | 42 | # Position the Label widget in the center of the window |
32 | | -clockTime.pack(anchor="center") |
| 43 | +clock_time.pack(anchor="center") |
33 | 44 |
|
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() |
36 | 47 |
|
37 | 48 | # Start the Tkinter main event loop |
38 | 49 | top.mainloop() |
0 commit comments