- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 COVID19 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 * window = Tk() # Creating the GUI window window.title("COVID-19 Tracker") # Setting the size of the window window.geometry('250x100') # Adding labels to the window lbl_active = Label(window, text="Total Active Cases:") lbl_active.grid(column=0, row=0) lbl_confirmed = Label(window, text="Total Confirmed Cases:") lbl_confirmed.grid(column=0, row=1) lbl_active_value = Label(window, text="") lbl_active_value.grid(column=1, row=0) lbl_confirmed_value = Label(window, text="") lbl_confirmed_value.grid(column=1, row=1) lbl_updated = Label(window, text="") lbl_updated.grid(column=0, row=2, columnspan=2) # Function to fetch and display the data def update_data(): url = "https://api.covid19india.org/data.json" response = requests.get(url) data = json.loads(response.text) lbl_active_value.configure(text=data["statewise"]["active"]) lbl_confirmed_value.configure(text=data["statewise"]["confirmed"]) lbl_updated.configure(text="Data last updated on " + data["statewise"]["lastupdatedtime"]) # Adding a button to fetch new data btn_refresh = Button(window, text="Refresh Data", command=update_data) btn_refresh.grid(column=0, row=3, columnspan=2) # Calling the update_data function to display initial data update_data() window.mainloop()
The first step is to import the necessary libraries − requests, json and tkinter.
Next, a tkinter window is created by instantiating the Tk() class. The title of the window is set to "COVID−19 Tracker" and the geometry of the window is set to 250x100 pixels.
Labels are added to the window to display the total number of active cases and confirmed cases. Two additional labels are added to display the values of these cases.
A blank label is added to the window to display the last updated time of the data.
The update_data() function is defined, which fetches data from the API, parses the JSON data, and updates the values of the labels displaying the total number of active cases, confirmed cases, and the last updated time.
A button is added to the window to trigger the update_data() function when clicked.
The update_data() function is called at the end to display the initial data.
Finally, the mainloop() method is called to display the GUI window and wait for user interactions.
In order to run the code you need to run the commands shown below.
pip install requests pip install tkinter
When the program is run, a GUI window titled "COVID−19 Tracker" appears with two labels displaying the total number of active cases and the total number of confirmed cases. Below these labels is a blank space where the updated time of the data is displayed.
There is a button labelled "Refresh Data" below this blank space. Clicking this button fetches new data from the API and updates the values of the two labels and the text of the "Last updated" label.
This program can be useful for individuals who want to keep track of the spread of COVID−19 in their region or around the world. By modifying the code, users can customize the GUI to display additional information or to track other data points related to the pandemic.
Kickstart Your Career
Get certified by completing the courseGet Started