-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigital_clock_gui.py
More file actions
88 lines (66 loc) · 2.88 KB
/
Copy pathdigital_clock_gui.py
File metadata and controls
88 lines (66 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# This file is part of the Python Projects repository, which is licensed under the
# Apache License, Version 2.0. You may obtain a copy of the license at
#
# http://www.apache.org/licenses/LICENSE-2.0
"""
Digital Clock using GUI Program.
Input:
- No specific input required from the user.
Output:
- Displays a digital clock on a graphical user interface.
Features:
- Displays a digital clock in a graphical user interface.
- Allows the user to toggle between 12-hour and 24-hour time formats.
- Supports the option to display or hide the date.
- Provides a simple and intuitive user interface for viewing the time.
- Updates the clock display in real-time.
"""
# Import necessary modules from the tkinter library.
import tkinter as tk
from tkinter import font
import time
class DigitalClock:
def __init__(self, root) -> None:
self.root = root
self.root.title("Clock")
self.root.configure(bg="#000000") # Set the background color to black
self.root.geometry("400x250") # Set the initial window size
# Font settings for digital clock display
self.digital_font = font.Font(family='Boulder', size=40)
# Variables
self.show_date = False
self.time_format_var = tk.StringVar()
self.time_format_var.set("12")
# Create digital clock frame
self.digital_clock_frame = tk.Frame(self.root, bg="#000000")
self.digital_clock_frame.pack(expand=True)
# Create widgets in the digital clock frame
self.label = tk.Label(self.digital_clock_frame, font=self.digital_font, bg="#000000", fg="#00FF00", bd=25)
self.label.pack(expand=True)
self.date_label = tk.Label(self.digital_clock_frame, font=('Boulder', 20), bg="#000000", fg="#00FF00")
self.date_label.pack(expand=True)
self.toggle_format_button = tk.Button(root, text="Toggle Format", command=self.toggle_time_format)
self.toggle_format_button.pack(pady=5)
self.toggle_date_button = tk.Button(root, text="Toggle Date", command=self.toggle_date_display)
self.toggle_date_button.pack(pady=5)
# Run the clock
self.update_clock()
def toggle_time_format(self):
current_format = self.time_format_var.get()
new_format = "12" if current_format == "24" else "24"
self.time_format_var.set(new_format)
def toggle_date_display(self):
self.show_date = not self.show_date
def update_clock(self):
time_live = time.strftime(f"%{'I' if self.time_format_var.get() == '12' else 'H'}:%M:%S")
self.label.config(text=time_live)
if self.show_date:
date = time.strftime("%Y-%m-%d")
self.date_label.config(text=f"Date: {date}")
else:
self.date_label.config(text="")
self.label.after(200, self.update_clock)
if __name__ == '__main__':
root = tk.Tk()
app = DigitalClock(root)
root.mainloop()