As we know Tkinter is used to create a variety of GUI (Graphical User Interface) applications. In this article we will learn how to create a Digital clock using Tkinter.
Prerequisites:
- Python functions
- Tkinter basics (Label Widget)
- Time module
Using Label widget from Tkinter and time module:
In the following application, we are going to use Label widget and also going to use time module which we will use to retrieve system’s time.
Below is the implementation:
Python3
from tkinter import *
from tkinter.ttk import *
from time import strftime
root = Tk()
root.title( 'Clock' )
def time():
string = strftime( '%H:%M:%S %p' )
lbl.config(text = string)
lbl.after( 1000 , time)
lbl = Label(root, font = ( 'calibri' , 40 , 'bold' ),
background = 'purple' ,
foreground = 'white' )
lbl.pack(anchor = 'center' )
time()
mainloop()
|
Output:

Code Explanation:
- The code starts by importing the necessary modules.
- The first module is the tkinter library, which provides basic functionality for creating graphical user interfaces (GUIs).
- Next, the strftime function is imported to retrieve system time.
- Next, a window is created and given a title of “Clock.”
- A function called time() is then created to display the current time on the label widget.
- This function uses the strftime() function to format the time string according to system conventions.
- The last part of this code sets up styling for the label widget so that it will look nicer.
- Finally, an instance of Label is created and placed at the center of the window.
- The time() function is executed, and your output should look like this: Clock: Tue Dec 12 08:00:00 2016
- The code creates a window and assigns it the title “Clock”.
- The time() function is then called to display the current time on the label widget.
- The lbl.config() function is used to set the text of the label widget.
- The after() function is used to delay displaying the time for 1000 milliseconds.
- Finally, the style of the label widget is modified with lbl.pack().
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 Nov, 2022
Like Article
Save Article