How to Create a COVID19 Data Representation GUI in Python?

The COVID-19 pandemic has disrupted daily life around the world, with numerous countries implementing lockdowns and other restrictions to control the spread of the virus. As a result, there is a great deal of interest in tracking the spread of the virus, including the number of active cases and confirmed cases. With the help of technology, it is now possible to access this data and visualize it in real-time using graphical user interfaces (GUIs). This tutorial will provide an overview of a Python program that creates a GUI for displaying COVID-19 data.

Creating a Tkinter GUI to Show COVID-19 Data

This Python program demonstrates how to create a GUI for displaying COVID-19 data using the tkinter library. The program fetches data from an API and updates the GUI with the latest information on the total number of active cases, total number of confirmed cases and last updated time.

The following code creates a GUI using the tkinter library in Python. The GUI includes two labels and a button. The first label displays the total number of active cases, while the second label displays the total number of confirmed cases. The button allows users to refresh the data and display updated information ?

import requests
import json
from tkinter import *

# Creating the GUI window
window = Tk()
window.title("COVID-19 Tracker")
window.geometry('350x150')

# Adding labels to the window
lbl_active = Label(window, text="Total Active Cases:")
lbl_active.grid(column=0, row=0, padx=10, pady=5, sticky="w")

lbl_confirmed = Label(window, text="Total Confirmed Cases:")
lbl_confirmed.grid(column=0, row=1, padx=10, pady=5, sticky="w")

lbl_active_value = Label(window, text="Loading...", fg="blue", font=("Arial", 10, "bold"))
lbl_active_value.grid(column=1, row=0, padx=10, pady=5)

lbl_confirmed_value = Label(window, text="Loading...", fg="red", font=("Arial", 10, "bold"))
lbl_confirmed_value.grid(column=1, row=1, padx=10, pady=5)

lbl_updated = Label(window, text="", fg="gray", font=("Arial", 8))
lbl_updated.grid(column=0, row=2, columnspan=2, padx=10, pady=5)

# Function to fetch and display the data
def update_data():
    try:
        url = "https://api.covid19india.org/data.json"
        response = requests.get(url, timeout=10)
        data = json.loads(response.text)
        
        active_cases = data["statewise"][0]["active"]
        confirmed_cases = data["statewise"][0]["confirmed"]
        last_updated = data["statewise"][0]["lastupdatedtime"]
        
        lbl_active_value.configure(text=f"{active_cases:,}")
        lbl_confirmed_value.configure(text=f"{confirmed_cases:,}")
        lbl_updated.configure(text=f"Data last updated: {last_updated}")
        
    except Exception as e:
        lbl_active_value.configure(text="Error")
        lbl_confirmed_value.configure(text="Error")
        lbl_updated.configure(text="Failed to fetch data")

# Adding a button to fetch new data
btn_refresh = Button(window, text="Refresh Data", command=update_data, 
                    bg="lightblue", font=("Arial", 10))
btn_refresh.grid(column=0, row=3, columnspan=2, pady=10)

# Calling the update_data function to display initial data
update_data()

window.mainloop()

How It Works

  • Import Libraries: The program imports requests for API calls, json for data parsing, and tkinter for GUI creation.

  • Window Setup: Creates a tkinter window with title "COVID-19 Tracker" and sets the window size to 350x150 pixels for better visibility.

  • Labels Creation: Adds labels to display active cases, confirmed cases, and their corresponding values with improved formatting and colors.

  • Data Fetching: The update_data() function fetches COVID-19 data from the API, parses JSON response, and updates the GUI labels.

  • Error Handling: Includes try-except block to handle network errors and API failures gracefully.

  • Refresh Button: Provides a button to manually refresh the data and display updated statistics.

Installation Requirements

Before running the code, install the required packages ?

pip install requests

Note: tkinter comes pre-installed with Python, so no separate installation is needed.

Output

When the program runs, a GUI window titled "COVID-19 Tracker" appears with:

  • Two labeled fields showing total active cases and confirmed cases with formatted numbers

  • A timestamp showing when the data was last updated

  • A "Refresh Data" button to fetch the latest statistics

  • Error handling that displays "Error" if the API is unavailable

Key Features

Feature Description
Real-time Data Fetches live COVID-19 statistics from API
User-friendly GUI Clean interface with tkinter widgets
Error Handling Gracefully handles network and API errors
Refresh Capability Manual data refresh with button click

Conclusion

This COVID-19 tracker GUI provides a simple yet effective way to monitor pandemic statistics using Python and tkinter. The program demonstrates API integration, error handling, and GUI development concepts that can be extended to track other data sources or add additional features like charts and regional breakdowns.

Updated on: 2026-03-27T11:08:02+05:30

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements