关于debian:使用Python查询国内-COVID19-疫情

7次阅读

共计 2068 个字符,预计需要花费 6 分钟才能阅读完成。

有时咱们只是想要一个速的工具来通知以后疫情的状况,咱们只须要起码的数据。应用 Python 语言和 tkinter 图形化显示数据。首先,咱们应用 Tkinter 库使咱们的脚本能够图形化显示。应用 requests 库从 丁香园 获取数据。而后咱们将在这种状况下显示咱们须要的数据“以后确诊人数”,“总确诊人数”。需要和装置 ssh 客户端为:MobaXterm,因为它反对 X11-Forwarding 零碎:Centos8 MinimalPython 版本:Python3.6.8 须要用到的库:requests, json, tkinter, time 上面来装置 xorg,用来在近程终端中关上图形化界面,装置 python3-tkinter 来创立 GUI 界面:[root@localhost data]# yum -y install xorg-x11-xauth xorg-x11-utils python3-tkinter
上脚本上面创立 python 脚本:[root@localhost data]# touch gui.py
[root@localhost data]# chmod +x gui.py
[root@localhost data]# vim gui.py

!/usr/bin/python3

导入 time, requests, json, tkinter 库

import time
import requests
import json
from tkinter import *

创立一个窗口

window = Tk()

窗口题目为“Covid-19”

window.title(“Covid-19”)

窗口大小为 600px * 130px

window.geometry(‘600×130’)

创立 label 1,并创立初始信息

lbl = Label(window, text = “The number of confirmed cases in China: ….”)

label 窗格占用第 1 列,第 1 行,靠左对齐。

lbl.grid(column = 1, row = 0, sticky = W)

创立 label 2,并创立初始信息

lbl1 = Label(window, text = “Cumulative number of confirmed cases in China: ….”)
lbl1.grid(column = 1, row = 1, sticky = W)

创立 label 3,并创立初始信息,用来显示工夫的。

lbl2 = Label(window, text = “Data update time: ….”)
lbl2.grid(column = 1, row = 3, sticky = W)

创立 label 4,并创立初始信息,用来显示是否刷新。

lbl3 = Label(window, fg = ‘green’, text = “”)
lbl3.grid(column = 1, row = 4, sticky = W)

定义一个点击事件

def clicked():

# 制订 API 接口的地址
url = "https://lab.isaaclin.cn/nCoV/api/overall"
# 获取 url 地址
page = requests.get(url)
# 将 json 数据载入内存
data = json.loads(page.text)
#上面 4 个变量用来将 API 中的工夫戳转化为工夫。tm = data["results"][0]["updateTime"]
timeStamp = float(tm/1000)
timeArray = time.localtime(timeStamp)
updatetime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

# 当点击时,获取 currentConfirmedCount 的值,因为 text 外面只能用 str 字符串,所以须要将证书类型的数字转化为字符串。lbl.configure(text ="The number of confirmed cases in China:" + "%s" %(data["results"][0]["currentConfirmedCount"]))

lbl1.configure(text ="Cumulative number of confirmed cases in China:" + "%s" %(data["results"][0]["confirmedCount"]))

lbl2.configure(text ="Data update time:" + updatetime)

lbl3.configure(text ="State: Refresh")

创立一个按钮,用来刷新数据。

btn = Button(window, text =”Refresh”, command = clicked)

按钮的地位在第 2 列,第 5 行。

btn.grid(column = 2, row = 5)

显示窗口

window.mainloop()
解释:sticky = 对齐形式的值有四个,N, S, W, E,代表着东西南北、上下左右。执行脚本,输入如下内容:[root@localhost data]# ./gui.py

点击 Refresh 之后,会看到获取的数据啦。

正文完
 0