|
| 1 | +from tkinter import Tk, Label |
| 2 | +from tkinter.font import Font |
| 3 | +import time |
| 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() |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + clock = DigitalClock() |
| 55 | + clock.start() |
| 56 | + |
0 commit comments