0

1) I have a problem with listbox,when user selected one of the country names in the listbox i want it to show the name as entry (default text).

2) after that when i click save button it shows error(actually,I don't know what is about).user could enter the name of the desire country(everything is possible).

3) I have an issue with a line that didn't get it.(i commented it below)

EDIT: 1) After founding my problems,i need to optimize my program.i don't know why it take so much time to get data from the site?

2) I need a label to show output when click on button"Show Data"

Thanks.

    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()
Nishef
  • 21
  • 4

2 Answers2

0

To achieve what you want, you need to use a Treeview widget.

Also, to make the treeview editable(with textboxes, like you said), see this answer.

Hope this helps!

10 Rep
  • 2,217
  • 7
  • 19
  • 33
0

So,i have found all of my problems...

Here is my final code:

from covid import *
from tkinter import *
from tkinter import messagebox
import tkinter as tk
import matplotlib.pyplot as plt
import pandas as pd
import pickle
import seaborn as sb

country_get = None


def main():
    global country_get
    win = Tk()
    win.geometry("300x400")
    win.title("")
    win.configure(bg="#fffaf0")
    win.resizable(False, False)
    #win.iconbitmap(r'D:/Python.p/Mine/Simple Projects/covid/py.ico')

    lbl_intro = Label(win, text="Covid-19 per country", font=("Arial", 16))
    lbl_intro.pack()

    covid = Covid()
    covid_general = Covid(source="worldometers")

    # def list_country():
    F1 = Frame(borderwidth=2, relief='ridge')
    F1.pack()
    lst_country = covid_general.list_countries()
    lst_country = sorted(lst_country)
    lb_countries = Listbox(F1, font=("times", 14), bg='#f5f5f5')

    s = Scrollbar(F1)
    lb_countries.pack(side=LEFT, expand=1, fill=X)
    s.pack(side=RIGHT, fill=Y)

    s['command'] = lb_countries.yview
    lb_countries['yscrollcommand'] = s.set
    #lb_countries.insert(END,"Country List:")
    for names in lst_country:
        lb_countries.insert(END, names)

    def get_country(*args):
        # gets which country is selected, and changes the label accordingly

        global country_get
        country_get = lst_country[lb_countries.curselection()[0]]

    lb_countries.bind("<<ListboxSelect>>", get_country)

    def save_data():

        country = covid.get_status_by_country_name(country_get)

        # filtering the data.using the online database
        data = {
            key: country[key]
            for key in country.keys() and {'confirmed', 'active', 'deaths', 'recovered'}

        }
        print(data)
        # print(country_general)
        messagebox.showinfo("Covid Data", data)
        # 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_get], columns=[
                            'active', 'deaths', 'recovered'])

        # filter data again with the numbers only.using dataframe
        df_b = df_a.loc[country_get]

        # 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=90)
        plt.legend(labels, loc="best")

        # fig.canvas.set_window_title('Window')
        plt.show()
    btn_save = Button(win, text="Show Data", padx=10, pady=4, fg='blue', font=(
        'arial', 12, 'bold'), bg='#e2e21d', command=save_data)
    btn_load = Button(win, text="Show Plot", padx=10, pady=4, font=(
        'arial', 12, 'bold'), bg='#e2e21d', fg='blue', command=load_data)
    btn_quit = Button(win, fg='white', padx=12, pady=8, font=('arial', 12, 'bold'), text="Exit",
                    command=lambda: win.quit(), bg='#0073cf')
    
    btn_quit.pack(side=BOTTOM)
    btn_save.pack(side=RIGHT)
    btn_load.pack(side=LEFT)

    win.mainloop()


if __name__ == "__main__":
    main()
Nishef
  • 21
  • 4