个性化阅读
专注于IT技术分析

如何创建GUI显示COVID19数据?

点击下载

先决条件: Python请求, Python GUI – Tkinter

有时我们只需要一个快速的工具来真正告知当前更新, 我们只需要很少的数据即可。 Web抓取处理是从Web上获取一些数据, 然后对其进行处理并以简短的方式显示相关内容。

代码在做什么?

  • 首先, 我们使用Tkinter库使脚本需要GUI
  • 我们正在使用请求库从非官方api获取数据
  • 然后, 在这种情况下, 我们将显示我们需要的数据:有效案例总数:和已确认案例

下面是实现。

import requests
import json
from tkinter import *
  
window = Tk()
  
# creating the Box
window.title( "Covid-19" )
  
# Determining the size of the Box
window.geometry( '220x70' )
  
# Including labels
lbl = Label(window, text = "Total active cases:-......" )
lbl1 = Label(window, text = "Total confirmed cases:-..." )
  
lbl.grid(column = 1 , row = 0 )
lbl1.grid(column = 1 , row = 1 )
lbl2 = Label(window, text = "")
lbl2.grid(column = 1 , row = 3 )
  
  
def clicked():
     # Opening the url and loading the
     # json data using json Library 
     url = "https://api.covid19india.org /data.json"
     page = requests.get(url)
     data = json.loads(page.text)
      
     lbl.configure(text = "Total active cases:-"
                   + data[ "statewise" ][ 0 ][ "active" ])
      
     lbl1.configure(text = "Total Confirmed cases:-"
                    + data[ "statewise" ][ 0 ][ "confirmed" ])
      
     lbl2.configure(text = "Data refreshed" )
  
btn = Button(window, text = "Refresh" , command = clicked)
btn.grid(column = 2 , row = 0 )
  
window.mainloop()

输出如下:

python-tkinter-covid19-gui

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


赞(0)
未经允许不得转载:srcmini » 如何创建GUI显示COVID19数据?

评论 抢沙发

评论前必须登录!