Hello guys, in this tutorial we will create a digital clock in Python.
Here, we will use Tkinter for the GUI. Without further delay, we will start building our application.
Let's start by creating an empty tkinter window.
from tkinter import *
import tkinter
cor1 = "#3d3d3d" # black
cor2 = "#fafcff" # white
cor3 = "#21c25c" # green
cor4 = "#eb463b" # red
cor5 = "#dedcdc" # gray
cor6 = "#3080f0" # blue
root = Tk()
root.title("")
root.geometry('440x180')
root.resizable(width=FALSE, height=FALSE)
root.configure(background=cor1)
root.mainloop()
In the code above, we created an empty window in Tkinter, and defined some colors that we will use for our application.
Then, we will import the datetime, which will allow us to work with the time and calendar.
from datetime import datetime
After that, we will obtain the hour, day of the week, 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")
Let's create a function and pass all of those values to that function.
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")
After that we'll create a label that will show the time.
l1 = Label(root, text="10:05:05", font=('Arial 80'), bg=cor1, fg=cor3)
l1.grid(row=0, column=0, sticky=NW, padx=5)
Now let's pass the hour value to the label we just created, and execute the functiondef 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)
Very well now, to keep the time dynamic, we will use the following within the function:l1.after(200, clock)
Now that our watch is already running, we will also pass the remaining values, creating a new label.l2 = Label(root, font=('Arial 20'), bg=cor1, fg=cor3)
l2.grid(row=1, column=0, sticky=NW, padx=5)
And within the clock function we will configure it as follows:l2.config(text=weekday + "" + str(day) + "/" + str(month) + "/" + (year))
First let's download the font here on this site. font https://www.1001fonts.com/digital-7-font.html
After downloading and extracting the sources, and placing it in the same directory where our python script is, we will add it to our application, but for that we will have to install another Python library first, which is:
After installation, write the following code to add the font we just downloaded.
import pyglet
pyglet.font.add_file('digital-7.ttf')
After importing and linking the font, just use the font and it will be active.Full code
from tkinter import *
import tkinter
from datetime import datetime
import pyglet
pyglet.font.add_file('digital-7.ttf')
cor1 = "#3d3d3d" # black
cor2 = "#fafcff" # white
cor3 = "#21c25c" # green
cor4 = "#eb463b" # red
cor5 = "#dedcdc" # gray
cor6 = "#3080f0" # blue
root = Tk()
root.title("")
root.geometry('300x150')
root.resizable(width=FALSE, height=FALSE)
root.configure(background=cor1)
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)
l2.config(text=weekday + "" + str(day) + "/" + str(month) + "/" + (year))
l1 = Label(root, text="10:05:05", font=('digital-7 80'), bg=cor1, fg=cor3)
l1.grid(row=0, column=0, sticky=NW, padx=5)
l2 = Label(root, font=('digital-7 20'), bg=cor1, fg=cor3)
l2.grid(row=1, column=0, sticky=NW, padx=5)
clock()
root.mainloop()
Post a Comment
0Comments