In this tutorial, we will walk through the process of creating a digital clock in Python using Tkinter for the graphical user interface (GUI). We’ll go step by step to ensure that you understand each part of the process. Before we dive into coding, don't forget to subscribe to our channel and like this video—your support is greatly appreciated!
Let’s start by setting up a basic Tkinter window for our clock application.
from tkinter import *
import tkinter
# Define some colors to style the clock
cor1 = "#3d3d3d" # black
cor2 = "#fafcff" # white
cor3 = "#21c25c" # green
cor4 = "#eb463b" # red
cor5 = "#dedcdc" # gray
cor6 = "#3080f0" # blue
# Create the main window
root = Tk()
root.title("Digital Clock")
root.geometry('440x180')
root.resizable(width=FALSE, height=FALSE)
root.configure(background=cor1)
root.mainloop()
In this code, we created an empty window using Tkinter and defined some colors that will be used in the styling of the clock. Now, let’s import the datetime
module, which will help us get the current time and calendar data.
from datetime import datetime
Next, we’ll extract the current time, weekday, day of the month, month, and year:
time = datetime.now()
hour = time.strftime("%H:%M:%S")
weekday = time.strftime("%A")
day = time.day
month = time.strftime("%b")
year = time.strftime("%Y")
Now, let’s create a function to update the clock with these values:
def clock():
time = datetime.now()
hour = time.strftime("%H:%M:%S")
weekday = time.strftime("%A")
day = time.day
month = time.strftime("%b")
year = time.strftime("%Y")
We will then display the time using a Tkinter label:
l1 = Label(root, text="10:05:05", font=('Arial 80'), bg=cor1, fg=cor3)
l1.grid(row=0, column=0, sticky=NW, padx=5)
To dynamically update the clock, we will configure the label to show the current time and refresh it every 200 milliseconds:
def clock():
time = datetime.now()
hour = time.strftime("%H:%M:%S")
weekday = time.strftime("%A")
day = time.day
month = time.strftime("%b")
year = time.strftime("%Y")
l1.config(text=hour)
l1.after(200, clock)
Next, let’s add another label to display the current date (weekday, day, month, and year):
l2 = Label(root, font=('Arial 20'), bg=cor1, fg=cor3)
l2.grid(row=1, column=0, sticky=NW, padx=5)
We’ll then update this label within the clock
function:
l2.config(text=weekday + " " + str(day) + "/" + str(month) + "/" + str(year))
At this point, the basic functionality of our clock is complete. Now, let's style the clock to make it look more digital. First, we’ll need a custom font. You can download a digital-style font from this site:
After downloading and extracting the font files, place them in the same directory as your Python script. To use this custom font in our application, we need to install the pyglet
library, which can be done with the following command:
pip install pyglet
Now, we can add the font to our application:
import pyglet
pyglet.font.add_file('digital-7.ttf')
Once the font is imported, we can use it in our labels.
Conclusion
Now you have a fully functional and stylish digital clock in Python using Tkinter. Feel free to experiment with the font, colors, and layout to further personalize your clock. If you found this tutorial helpful, make sure to subscribe and leave a like! Thanks for watching!
Copyright Statement: Unless stated otherwise, all articles are original to this site, please credit the source when sharing.
Article link:http://pybeginners.com/python-projects/how-to-build-a-digital-clock-in-python/
License Agreement:Attribution-NonCommercial 4.0 International License