|
7 | 7 | window.configure(bg="green")
|
8 | 8 | window.resizable(False, False)
|
9 | 9 |
|
| 10 | +use_24h = True |
| 11 | + |
10 | 12 | clock_label = Label(
|
11 | 13 | window, bg="black", fg="green", font=("Arial", 30, "bold"), relief="flat"
|
12 | 14 | )
|
13 | 15 | clock_label.place(x=50, y=50)
|
14 | 16 |
|
15 |
| -# NEW date label |
| 17 | + |
16 | 18 | date_label = Label(
|
17 | 19 | window, bg="black", fg="white", font=("Arial", 14)
|
18 | 20 | )
|
19 | 21 | date_label.pack(pady=(0, 10), anchor="center")
|
20 | 22 |
|
21 | 23 |
|
22 | 24 |
|
| 25 | +def toggle_format(_evt=None): |
| 26 | + global use_24h |
| 27 | + use_24h = not use_24h |
| 28 | + fmt_btn.config(text="Switch to 24-hour" if not use_24h else "Switch to 12-hour") |
| 29 | + |
| 30 | +fmt_btn = Button(window, text="Switch to 12-hour", command=toggle_format) |
| 31 | +fmt_btn.pack(pady=(0, 8)) |
| 32 | +window.bind("<f>", toggle_format) # press 'f' to toggle |
| 33 | + |
| 34 | + |
| 35 | + |
23 | 36 | def update_label():
|
24 |
| - current_time = strftime("%H: %M: %S\n %d-%m-%Y ") |
25 |
| - clock_label.configure(text=current_time) |
26 |
| - clock_label.after(80, update_label) |
27 |
| - clock_label.pack(anchor="center") |
| 37 | + |
| 38 | + if use_24h: |
| 39 | + time_text = strftime("%H:%M:%S") |
| 40 | + else: |
| 41 | + # strip leading zero in 12h mode for a cleaner look |
| 42 | + time_text = strftime("%I:%M:%S %p").lstrip("0") |
| 43 | + clock_label.configure(text=time_text) |
| 44 | + date_label.configure(text=strftime("%A, %b %d, %Y")) |
| 45 | + window.after(1000, update_label) |
28 | 46 |
|
29 | 47 | update_label()
|
30 |
| -window.mainloop() def update_label(): |
31 |
| - clock_label.configure(text=strftime("%H:%M:%S")) |
32 |
| - date_label.configure(text=strftime("%A, %b %d, %Y")) |
33 |
| - clock_label.after(1000, update_label) |
| 48 | +window.mainloop() |
| 49 | + |
34 | 50 |
|
0 commit comments