Create A Digital Clock with Python

Search for a command to run...

gald you liked it Pranav Bhattarai
The series content will mostly cover Technical Python tutorials and my recent learnings and experience in Python.
You all must have heard about or seen a lot of QR codes, I was thinking to create one for my blogging website and I decided to do the same with python. How cool right? Do you wish to know how to create one? Here's how you can do it! You can also ref...
All you need to know about Hacktoberfest 2021

Hello reader! Well, Hacktoberfest is something, you are gonna hear a lot about in the coming month. You can refer to my YouTube video Tutorial to see a working tutorial for better understanding and a step-by-step guide of the same. https://www.yo...

Hello reader! In this Blog post, I will share some useful code snippets and functions in Python. Give it a read. Let's get started! 1. Memory The method getsizeof can be used to retrieve the size of any object. Here's an example of the same. import...

Hello reader! Whenever a client sends a request to the server, the response always contains a status code. You might not see it always but it's returned at every client-server interaction. Well, even if you are not a programmer, you would still know...

Hello reader! Microsoft subsidiary GitHub launched Copilot to power pair programming with AI. In this blog post, I will share all you need to know about it. You can refer to my YouTube video Tutorial to see a working tutorial for better understandin...

Hi everyone,
In this tutorial, I am going to teach you how to build your own digital clock with Python. We will use Tkinter to do the same.
You can refer to my YouTube video tutorial for a better understanding.
Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!
Let's get started.
You can find all the code at my GitHub Repository.

Create a python file, I am naming it as demo.py. You can name it anything you want.
from tkinter import *
from tkinter.ttk import *
we have imported the tkinter , now lets import time. We require from time import strftime from time.
from time import strftime
Next, we need to create UI for our digital clock.
root = Tk()
Now we need to set the title of our clock. Let's set the title using title method.
root.title('Digital Clock')
Now, let's define a method to get the time. I will name it as clock. We will use strftime method to get the time and store it inside a string. Let's name the string as tick. Now it's time to provide the time format.
def clock():
tick = strftime('%H:%M:%S %p')
Now, let's set the label using config method.
label.config(text = tick)
Now, we can call our clock function and use the after method to do the same. We would like to call it every second, so that will be our first parameter.
label.after(1000, clock)
Once done with the title, we will require a label. The label will be used to store our title. so let's create a label. we will do the same using label method.
label = Label(root, font=('ds-digital', 100), background = 'black', foreground = 'red')
Let's design it.
Now that we have designed our label, we are ready to pack it. We will do so with the pack method. You can also define the alignment of the label using the anchor method.
label.pack(anchor='center')
Moving forward let's call our clock function and at the end we will cal mainloop.
```
That's it and we are done! It will look something like this!
You can find the code at my GitHub Repository
If you have any queries or suggestions, feel free to reach out to me.
You can connect with me on Twitter.
You should definitely check out my other Blogs:
See you in my next article, Take care!