|
| 1 | +# importing tkinter |
| 2 | +from tkinter import * |
| 3 | +# initializing tkinter |
| 4 | +root = Tk() |
| 5 | +# setting geometry |
| 6 | +root.geometry("350x350") |
| 7 | +# setting title |
| 8 | +root.title("Get Covid-19 Data Country Wise") |
| 9 | + |
| 10 | +# function which will get covid data and will show it |
| 11 | +def showdata(): |
| 12 | + # importing matplotlib which will be used to show data graphically |
| 13 | + from matplotlib import pyplot as plt |
| 14 | + # to scale the data we are importing patches |
| 15 | + import matplotlib.patches as mpatches |
| 16 | + # importing covid library |
| 17 | + from covid import Covid |
| 18 | + # initializing covid library |
| 19 | + covid = Covid() |
| 20 | + # declaring empty lists to store different data sets |
| 21 | + cases = [] |
| 22 | + confirmed = [] |
| 23 | + active = [] |
| 24 | + deaths = [] |
| 25 | + recovered = [] |
| 26 | + # using try and except to run program without errors |
| 27 | + try: |
| 28 | + # updating root |
| 29 | + root.update() |
| 30 | + # getting countries names entered by the user |
| 31 | + countries = data.get() |
| 32 | + # removing white spaces from the start and end of the string |
| 33 | + country_names = countries.strip() |
| 34 | + # replacing white spaces with commas inside the string |
| 35 | + country_names = country_names.replace(" ", ",") |
| 36 | + # splitting the string to store names of countries |
| 37 | + # as a list |
| 38 | + country_names = country_names.split(",") |
| 39 | + # for loop to get all countries data |
| 40 | + for x in country_names: |
| 41 | + # appending countries data one-by-one in cases list |
| 42 | + # here, the data will be stored as a dictionary |
| 43 | + # for one country i.e. for each country |
| 44 | + # there will be one dictionary in the list |
| 45 | + # which will contain the whole information |
| 46 | + # of that country |
| 47 | + cases.append(covid.get_status_by_country_name(x)) |
| 48 | + # updating the root |
| 49 | + root.update() |
| 50 | + # for loop to get one country data stored as dict in list cases |
| 51 | + for y in cases: |
| 52 | + # storing every Country's confirmed cases in the confirmed list |
| 53 | + confirmed.append(y["confirmed"]) |
| 54 | + # storing every Country's active cases in the active list |
| 55 | + active.append(y["active"]) |
| 56 | + # storing every Country's deaths cases in the deaths list |
| 57 | + deaths.append(y["deaths"]) |
| 58 | + # storing every Country's recovered cases in the recovered list |
| 59 | + recovered.append(y["recovered"]) |
| 60 | + # marking the color information on scaleusing patches |
| 61 | + confirmed_patch = mpatches.Patch(color='green', label='confirmed') |
| 62 | + recovered_patch = mpatches.Patch(color='red', label='recovered') |
| 63 | + active_patch = mpatches.Patch(color='blue', label='active') |
| 64 | + deaths_patch = mpatches.Patch(color='black', label='deaths') |
| 65 | + # plotting the scale on graph using legend() |
| 66 | + plt.legend(handles=[confirmed_patch, recovered_patch, active_patch, deaths_patch]) |
| 67 | + # showing the data using graphs |
| 68 | + # this whole for loop section is related to matplotlib |
| 69 | + for x in range(len(country_names)): |
| 70 | + plt.bar(country_names[x], confirmed[x], color='green') |
| 71 | + if recovered[x] > active[x]: |
| 72 | + plt.bar(country_names[x], recovered[x], color='red') |
| 73 | + plt.bar(country_names[x], active[x], color='blue') |
| 74 | + else: |
| 75 | + plt.bar(country_names[x], active[x], color='blue') |
| 76 | + plt.bar(country_names[x], recovered[x], color='red') |
| 77 | + plt.bar(country_names[x], deaths[x], color='black') |
| 78 | + # setting the title of the graph |
| 79 | + plt.title('Current Covid Cases') |
| 80 | + # giving label to x direction of graph |
| 81 | + plt.xlabel('Country Name') |
| 82 | + # giving label to y direction of graph |
| 83 | + plt.ylabel('Cases(in millions)') |
| 84 | + # showing the full graph |
| 85 | + plt.show() |
| 86 | + except Exception as e: |
| 87 | + # asking user to enter correct details |
| 88 | + # during entering the country names on GUI |
| 89 | + # please differentiate the country names |
| 90 | + # with spaces or comma but not with both |
| 91 | + # otherwise you will come to this section |
| 92 | + data.set("Enter correct details again") |
| 93 | + |
| 94 | + |
| 95 | +Label(root, text="Enter all countries names\nfor whom you want to get\ncovid-19 data", font="Consolas 15 bold").pack() |
| 96 | +Label(root, text="Enter country name:").pack() |
| 97 | +data = StringVar() |
| 98 | +data.set("Seperate country names using comma or space(not both)") |
| 99 | +entry = Entry(root, textvariable=data, width=50).pack() |
| 100 | +Button(root, text="Get Data", command=showdata).pack() |
| 101 | +root.mainloop() |
0 commit comments