【发布时间】:2020-09-10 13:33:48
【问题描述】:
1) 我的列表框有问题,当用户在列表框中选择一个国家/地区名称时,我希望它显示名称作为条目(默认文本)。
2) 之后,当我点击保存按钮时,它显示错误(实际上,我不知道是怎么回事)。用户可以输入所需国家的名称(一切皆有可能)。
3) 我有一个问题没有得到它。(我在下面评论了它)
编辑: 1)发现问题后,我需要优化我的程序。我不知道为什么要花这么多时间从网站获取数据?
2) 点击“显示数据”按钮时,我需要一个标签来显示输出
谢谢。
from covid import *
from tkinter import *
import tkinter as tk
import matplotlib.pyplot as plt
import pandas as pd
import pickle
import seaborn as sb
def main():
win=Tk()
win.geometry("400x400")
lbl=Label(win,text="Write ur country name:")
lbl.pack()
#print(country_name)
covid=Covid()
#covid_general=Covid(source="worldometers")
in_country=Entry(win)
in_country.pack()
country_name=in_country.get()
def list_country():
lst_country=covid.list_countries()
lbl_countries=Listbox(win,font=("times",14))
lbl_countries.pack(expand=1,fill=BOTH)
for names in lst_country:
lbl_countries.insert(END,names)
'''
for item in name:
in_country.insert(END,lbl_countries.get(item))
print(item)
'''
def save_data():
country=covid.get_status_by_country_name(country_name)
#what does this do?key:country[key]
data={
key:country[key]
for key in country.keys() and {'confirmed','active','deaths','recovered'}
}
#save the data to a file covid.pkl
a_file=open("covid.pkl","wb")
pickle.dump(data,a_file)
a_file.close()
def load_data():
#read the data from covid.pkl
a_file=open("covid.pkl","rb")
output_a=pickle.load(a_file)
#now get data to data frame
df_a=pd.DataFrame(output_a,index=[country_name],columns=['active','deaths','recovered'])
#filter data again with the numbers only.using dataframe
df_b=df_a.loc[country_name]
#check output
print(df_a)
#reforming pieplot
labels = ['active','deaths','recovered']
colors=['orange','red','green']
explode=(0,0.2,0)
#now plot the data
plt.title("Covid Chart")
main_data=plt.pie(df_a,explode=explode,labels=df_b,colors=colors,autopct='%1.1f%%',startangle=140)
plt.legend(labels,loc="upper left")
plt.show()
btn_save=Button(win,text="Save",command=save_data)
btn_load=Button(win,text="load_data",command=load_data)
btn_list=Button(win,text="Show Countries Name",command=list_country)
btn_save.pack()
btn_load.pack()
btn_list.pack()
win.mainloop()
if __name__=="__main__":
main()
【问题讨论】:
标签: python pandas matplotlib tkinter pickle