How to Create a Digital Clock Using Python

Posted

in

by

A digital clock is a very common type of clock that displays the time in digits, as opposed to an analog clock that displays the time using clock hands. With Python, you can easily create a digital clock using the Tkinter GUI toolkit. In this article, we will show you how to create a digital clock using Python.

Step 1: Importing Required Libraries We need to import the following libraries to create a digital clock in Python:

from tkinter import *
import time

Here, we import the Tkinter library for creating the graphical user interface, and the time library for working with time and dates in Python.

Step 2: Creating the Digital Clock Class We will create a class called DigitalClock that contains the code for creating the digital clock.

class DigitalClock:
    def __init__(self, root):
        self.root = root
        self.root.title("Digital Clock")
        self.root.geometry("300x200")
        self.root.resizable(False, False)

        # Create a label for displaying the current time
        self.time_label = Label(root, text="", font=("Arial", 50))
        self.time_label.pack(pady=20)

        # Add your website name with a hyperlink
        self.link_label = Label(root, text="exasub.com", font=("Arial", 14), fg="blue", cursor="hand2")
        self.link_label.pack(pady=10)
        self.link_label.bind("<Button-1>", lambda event: self.open_website())

        # Update the time every second
        self.update_clock()

Here, we define the __init__ method that initializes the class with the root argument. We set the title of the window to “Digital Clock”, and set the window size to 300×200. We then create a label called time_label to display the current time with a font size of 50.

We also create another label called link_label that displays your website name with a hyperlink. When the user clicks on the link, the open_website method will be called.

Finally, we call the update_clock method to update the time every second.

Step 3: Updating the Clock We will now define the update_clock method that will update the clock every second.

def update_clock(self):
    # Get the current time and format it
    current_time = time.strftime("%H:%M:%S")
    self.time_label.config(text=current_time)

    # Schedule the next update after 1 second
    self.root.after(1000, self.update_clock)

Here, we get the current time using the strftime method of the time library, and format it in the HH:MM:SS format. We then set the text of the time_label to the current time using the config method.

We also schedule the next update after 1 second using the after method of the root window.

Step 4: Opening the Website We will define the open_website method that will open your website when the user clicks on the hyperlink.

def open_website(self):
    import webbrowser
    webbrowser.open_new("http://www.exasub.com")

Here, we import the webbrowser library and use the open_new method to open your website.

Step 5: Running the Program Finally, we will create the main method and run the program.

if __name__ == "__main__":
    root = Tk()
    clock = DigitalClock(root)
    root.mainloop()

Here is the complete code for creating a digital clock using Python:

from tkinter import *
import time

class DigitalClock:
    def __init__(self, root):
        self.root = root
        self.root.title("Digital Clock")
        self.root.geometry("300x200")
        self.root.resizable(False, False)

        # Create a label for displaying the current time
        self.time_label = Label(root, text="", font=("Arial", 50))
        self.time_label.pack(pady=20)

        # Add your website name with a hyperlink
        self.link_label = Label(root, text="exasub.com", font=("Arial", 14), fg="blue", cursor="hand2")
        self.link_label.pack(pady=10)
        self.link_label.bind("<Button-1>", lambda event: self.open_website())

        # Update the time every second
        self.update_clock()

    def update_clock(self):
        # Get the current time and format it
        current_time = time.strftime("%H:%M:%S")
        self.time_label.config(text=current_time)

        # Schedule the next update after 1 second
        self.root.after(1000, self.update_clock)

    def open_website(self):
        import webbrowser
        webbrowser.open_new("http://www.exasub.com")

if __name__ == "__main__":
    root = Tk()
    clock = DigitalClock(root)
    root.mainloop()

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *