|
1 | | -import tkinter as tk |
2 | | -import time |
| 1 | +from tkinter import Tk, Label |
3 | 2 | from tkinter.font import Font |
| 3 | +import time |
| 4 | + |
4 | 5 |
|
| 6 | +class DigitalClock: |
| 7 | + def __init__(self, font=None): |
| 8 | + """Initialize the digital clock.""" |
| 9 | + self.create_window() |
| 10 | + self.configure_window() |
| 11 | + self.set_font(font) |
| 12 | + self.add_header() |
| 13 | + self.add_clock() |
| 14 | + self.update_time_on_clock() |
| 15 | + |
| 16 | + def create_window(self): |
| 17 | + """Create the main window.""" |
| 18 | + self.window = Tk() |
| 19 | + |
| 20 | + def configure_window(self): |
| 21 | + """Configure the main window properties.""" |
| 22 | + self.window.title('Digital Clock') |
| 23 | + self.window.config(bg='black') |
| 24 | + |
| 25 | + def set_font(self, customFont): |
| 26 | + """Set the font for the clock display.""" |
| 27 | + DEFAULT_FONT = Font(family='Arial', size=90, weight='normal') |
| 28 | + self.font = customFont if customFont is not None else DEFAULT_FONT |
| 29 | + |
| 30 | + def add_header(self): |
| 31 | + """Add a header label to the window.""" |
| 32 | + self.header = Label(self.window, text='Time Clock', |
| 33 | + font=self.font, bg='gray', fg='white') |
| 34 | + self.header.grid(row=1, column=2) |
| 35 | + |
| 36 | + def add_clock(self): |
| 37 | + """Add the clock label to the window.""" |
| 38 | + self.clock = Label(self.window, font=( |
| 39 | + 'times', 90, 'bold'), bg='blue', fg='white') |
| 40 | + self.clock.grid(row=2, column=2, padx=620, pady=250) |
| 41 | + |
| 42 | + def update_time_on_clock(self): |
| 43 | + """Update the time displayed on the clock every second.""" |
| 44 | + currentTime = time.strftime("%H:%M:%S") |
| 45 | + self.clock.config(text=currentTime) |
| 46 | + self.clock.after(1000, self.update_time_on_clock) |
| 47 | + |
| 48 | + def start(self): |
| 49 | + """Start the Tkinter main loop.""" |
| 50 | + self.window.mainloop() |
5 | 51 |
|
6 | 52 |
|
7 | | -def clocks(): |
8 | | - ''' |
9 | | - This function displays the current time. |
10 | | - ''' |
11 | | - currentTime = time.gmtime() |
12 | | - currentTime = f'{currentTime.tm_hour}:{currentTime.tm_min}:{currentTime.tm_sec}' |
13 | | - clock.config(text=currentTime) |
14 | | - clock.after(200, clocks) |
15 | | - ''' |
16 | | -This is simply printing out to verify whether it is functioning correctly or not. |
17 | | - ''' |
18 | | - print("Working Properly") |
19 | | - |
20 | | - |
21 | | -''' |
22 | | -Here I created a instance of TK class |
23 | | -''' |
24 | | -window = tk.Tk() |
25 | | -# creating a title |
26 | | -window.title("Digital Clock") |
27 | | -# creating background color |
28 | | -window.config(bg="black") |
29 | | - |
30 | | -font1 = Font(family="Arial", size=90, weight="normal") |
31 | | -# creating a header using Label Widget |
32 | | -header = tk.Label(window, text="Time Clock", font=font1, bg="gray", fg="white") |
33 | | -# using grid() to place the header somewhere comfortable in the gui |
34 | | -header.grid(row=1, column=2) |
35 | | -''' |
36 | | -A clock is being constructed using the Label Widget, which exhibits the current time and is positioned using grid. |
37 | | -''' |
38 | | -clock = tk.Label(window, font=("times", 90, "bold"), bg="blue", fg='white') |
39 | | -clock.grid(row=2, column=2, padx=620, pady=250) |
40 | | - |
41 | | -''' |
42 | | -
|
43 | | -calling the function here |
44 | | -''' |
45 | | -clocks() |
46 | | - |
47 | | -''' |
48 | | -running the main window |
49 | | -''' |
50 | 53 | if __name__ == "__main__": |
| 54 | + clock = DigitalClock() |
| 55 | + clock.start() |
51 | 56 |
|
52 | | - window.mainloop() |
|
0 commit comments