DropDown option to show subset of dataframe Tkinter

大憨熊 提交于 2020-03-06 09:32:46

问题


I have a dataframe pandas of which i want to make GUI to display the data , i have date_time one column which show data every one hour interval i want to make a dropdown option suppose if user select 1 hours then only the 1 hours all columns & rows show , if user select 2 hour then second all columns & rows display . Cany any one please help me how to display the data gving dropdown option. I will really Appreciate it. Thanks in Advance.

SAMPLE DATA:

Name:   Longitude(degree)   Latitude(degree)    DATE_TIME   Mean Sea Value (m)  DRY or WET
SD      87.0308            21.4441    00:00 IST 05-08-2019    -0.0467     DRY
Sea1    87.0544            21.4152    00:00 IST 05-08-2019    -1.0653     DRY
4K      86.9927            21.4197    00:00 IST 05-08-2019    -0.1331     DRY
4KP1    86.9960            21.4166    00:00 IST 05-08-2019    -0.0863     DRY
Name:   Longitude(degree)   Latitude(degree)    DATE_TIME   Mean Sea Value (m)  DRY or WET
SD      87.0308          21.4441      01:00 IST 05-08-2019    -0.0329     DRY
Sea1    87.0544          21.4152      01:00 IST 05-08-2019    -0.4067     DRY
4K      86.9927          21.4197      01:00 IST 05-08-2019    -0.0897     DRY
4KP1    86.9960           21.4166     01:00 IST 05-08-2019    -0.0676     DRY

回答1:


Minimal example how to use OptionMenu to filter data in DataFrame

I will not try to display it in tkinter because it is different problem.


I create list with values which I will display in OptionMenu

values = ['all'] + list(df['TIME'].unique())

and variable which I will use to get selection after pressing button

selected = tk.StringVar()

When I get selection then I can use

df2 = df[ filter ]

to select only some rows.


If you need something different then you have to better describe problem and show some code which can be used to test and modifications.


import tkinter as tk
import pandas as pd

# --- functions ---

def on_click():
    val = selected.get()
    if val == 'all':
        print(df)
    else:        
        df2 = df[ df['TIME'] == val ]
        print(df2)

# --- main ---

df = pd.DataFrame({
    'TIME': ['00:00','00:00','01:00','01:00','02:00','02:00'],
    'A': ['a','b','c','d','e','f'],
    'B': ['x','x','y','y','z','z'],
})

root = tk.Tk()

values = ['all'] + list(df['TIME'].unique())
selected = tk.StringVar()

options = tk.OptionMenu(root, selected, *values)
options.pack()

button = tk.Button(root, text='OK', command=on_click)
button.pack()

root.mainloop()  

EDIT: version with data displayed in GUI - not ideal but it shows all or filtered data.

import tkinter as tk
import pandas as pd

# --- functions ---

def showdata():
    global table

    # destroy old frame with table
    if table:
        table.destroy()

    # create new frame with table         
    table = tk.Frame(frame_data)
    table.grid(row=0, column=0)

    # fill frame with table
    row, column = df2.shape
    for r in range(row):
        for c in range(column):
            e1 = tk.Entry(table)
            e1.insert(1, df2.iloc[r, c])
            e1.grid(row=r, column=c, padx=2, pady=2)
            e1.config(state='disabled')

def on_click():
    global df2

    val = selected.get()

    if val == 'all':
        df2 = df
        #next_button.grid_forget()
    else:
        df2 = df[ df['TIME'] == val ]
        #next_button.grid(row=1, column=0)

    print(df2)
    showdata()
    next_button.grid(row=1, column=0)

# --- main ---

frame_data = None

df = pd.DataFrame({
    'TIME': ['00:00','00:00','01:00','01:00','02:00','02:00'],
    'A': ['a','b','c','d','e','f'],
    'B': ['x','x','y','y','z','z'],
})

root = tk.Tk()

values = ['all'] + list(df['TIME'].unique())
selected = tk.StringVar()

options = tk.OptionMenu(root, selected, *values)
options.pack()

button = tk.Button(root, text='OK', command=on_click)
button.pack()

# frame for table and button "Next Data"
frame_data = tk.Frame(root)
frame_data.pack()

exit_button = tk.Button(root, text="EXIT", command=root.destroy)
exit_button.pack() 

# table with data - inside "frame_data" - without showing it
table = tk.Frame(frame_data)
#table.grid(row=0, column=0)

# buttom "Next Data" - inside "frame_data" - without showing it
next_button = tk.Button(frame_data, text="Next Data", command=showdata)
#next_button.grid(row=1, column=0)

root.mainloop()


来源:https://stackoverflow.com/questions/59890350/dropdown-option-to-show-subset-of-dataframe-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!