Creating a Basic Live Clock Application with Tkinter in Python

Posted by

Simple Live Clock App using Tkinter in Python

Simple Live Clock App using Tkinter in Python

Python is a powerful programming language that is widely used for developing a wide range of applications. In this article, we will explore how to create a simple live clock app using Tkinter, the standard GUI toolkit for Python.

First, make sure you have Python installed on your computer. You can check if you have Python by opening a terminal and typing python --version. If you don’t have Python, you can download it from the official website and follow the installation instructions.

Next, open your favorite text editor and create a new Python file. In this file, we will write the code for our live clock app using Tkinter.

Here’s the code for our simple live clock app:


import tkinter as tk
from time import strftime

root = tk.Tk()
root.title("Simple Live Clock App")

def time():
    string = strftime('%H:%M:%S %p')
    label.config(text=string)
    label.after(1000, time)

label = tk.Label(root, font=('calibri', 40, 'bold'), background='black', foreground='white')
label.pack(anchor='center')
time()

root.mainloop()
	
	

Save the file with a .py extension, such as live_clock.py, and run it using the command python live_clock.py. You should see a window displaying the current time in a live clock format.

Let’s break down the code. We start by importing the tk module from the tkinter library and the strftime function from the time module. Then, we create a Tkinter window and set its title to “Simple Live Clock App”.

We define a function called time that gets the current time using strftime and updates the label’s text to display the current time. We also set the label’s font, background, and foreground colors.

Finally, we create a label widget and call the time function to update the time every second using the after method.

With just a few lines of code, we have created a simple live clock app using Tkinter in Python. You can further customize the app’s appearance and functionality to suit your needs.

I hope you found this tutorial helpful in creating your own live clock app using Tkinter in Python. Have fun coding!