In this Python tutorial, we will see how to create a digital clock using python turtle. This digital clock can be created with the help of Graphical User interface application. This tkinter is used to create a variety of GUI( Graphical User Interface) application.
How to create digital clock using Python Tkinter
Here we are creating the digital clock for this we are using the label widget and the time module that is used to recover the system time and created a digital clock with a beautiful background and foreground color.
Block of Code:
In this Python tkinter block of code we are importing all the necessary library. Here firstly we are importing the tkinter library that gives the functionality for building the graphical user interface and using the strftime function that is imported for recovering the system time.
#How to create digital clock using Python Tkinter
# importing whole module
from tkinter import *
from tkinter.ttk import *
# importing strftime function to recover the system's time
from time import strftime
Github Link
Check this code in the Repository from Github and you can also fork this code.
Github User Name: PythonT-Point
Block of Code:
In this python tkinter block of code, here we are creating a window and give the title to the window as Pythontpoint and then creating a function called time that is used to display the current time on the label widget.
After that uses the strftime() function that form the time string according to the system convention.
#How to create digital clock using Python Tkinter
# creating tkinter window
ws = Tk()
ws.title('Pythontpoint')
# This function is used to display the time on the label
def digitalclocktime():
strng = strftime('%H:%M:%S %p')
label.config(text=strng)
label.after(1000, digitalclocktime)
Block of Code:
In this block of code we are describing the styling for the label widget that give the window a style and the clock look attractive.
Here the sample of the label is created and it is placed at the center of the window. The time() function is executed and the digital clock is created.
The whole code creates a window and the title is also given to the window. The time() function is also called to show current time on the label widget.
In this code we are using the after() function that is used to delay the displaying time for 100 milliseconds and also giving the style to the widget that is modified by label.pack()
# Style the label widget so that clock will look more attractive
label = Label(ws, font=('Times New Roman', 50, 'bold'),
background='cyan',
foreground='white')
# Place the clock at the center of the tkinter window
label.pack(anchor='center')
digitalclocktime()
mainloop()
Code:
Hereafter splitting the code and explaining how to create digital clock using Python, now we will see how the output look like after running the whole code.
# How to create digital clock using Python Tkinter
# importing whole module
from tkinter import *
from tkinter.ttk import *
# importing strftime function to recover the system's time
from time import strftime
# creating tkinter window
ws = Tk()
ws.title('Pythontpoint')
# This function is used to display the time on the label
def digitalclocktime():
strng = strftime('%H:%M:%S %p')
label.config(text=strng)
label.after(100, digitalclocktime)
# Style the label widget so that clock will look more attractive
label = Label(ws, font=('Times New Roman', 50, 'bold'),
background='cyan',
foreground='white')
# Place the clock at the center of the tkinter window
label.pack(anchor='center')
digitalclocktime()
mainloop()
Output:
After running the whole code we get the following output in which we can see that the digital clock is created and generated the exact time as a clock. Here we are creating a window and give the title to the window as Pythontpoint and then creating a function called time that is used to display the current time on the label widget.
So, in this tutorial, we have illustrated How to create digital clock using Python Tkinter. Moreover, we have also discussed the whole code used in this tutorial.
Read some more tutorials related to Python Tkinter
Python Tkinter temperature converter
Python Tkinter Simple Calculator
Stone Paper Scissor using Python Tkinter
Python Tkinter Instagram User Detail