-1

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
MSA msa
  • 83
  • 1
  • 10
  • create dropdown with options, get value from dropdown and use it in pandas to select rows which you want to display, and then display them. See: [OptionMenu](http://effbot.org/tkinterbook/optionmenu.htm) – furas Jan 24 '20 at 04:30
  • Any Example similar ? How to pass rows & columns as variable in dropdown , I have more than 400 rows with 6 columns . – MSA msa Jan 24 '20 at 04:38
  • in link you have example how to use OptionMenu. Simply get value from OptionMenu and use it with pandas - `df2 = df1[ filter ]` - and then display `df2`. Frankly I don't understand what you want to select. If you want better example then first create minimal working code which we could run and modify. – furas Jan 24 '20 at 04:42

1 Answers1

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()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you very much , i really appreciate your help , But i want the all rows & columns data to show when we select time 00 in dropdown then it display all the rows & columns data of 00 time , Please let me know how to proceed that , rows & columns of 00 time can be in entery or label any . Thank you. – MSA msa Jan 24 '20 at 06:16
  • but my example do this. when you select time `01:00` then it display (`print()`) all rows & columns which have time `01:00` – furas Jan 24 '20 at 06:22
  • Actually i want to see the data on GUI not on terminal , For that my try is below which is giving error in Entry **_tkinter.TclError: bad option "-row": must be -after, -anchor, -before, -expand, -fill, -in, -ipadx, -ipady, -padx, -pady, or -side** using Entry – MSA msa Jan 24 '20 at 06:30
  • else: df2 = df[ df['TIME'] == val ] print(df2) row, column = df.shape for r in range(row): for c in range(column): e1 = tk.Entry(root) e1.insert(1, df.iloc[r, c]) e1.pack(row=r, column=c) e1.config(state='disabled') – MSA msa Jan 24 '20 at 06:34
  • as I wrote in answer: displaying in GUI is different big problem. – furas Jan 24 '20 at 06:45
  • there are different layout managers - `pack()`, `grid()`, `place()`. And `grid()` use `"row"` but `pack()`, `place()` don't use `"row"` – furas Jan 24 '20 at 06:48
  • BTW: you should create function which get dataframe and create table with `Entry` - this way you could run it with different dataframes - full dataframe or filtered. – furas Jan 24 '20 at 06:53
  • I added example which display data in GUI. – furas Jan 24 '20 at 09:43
  • Thanks for your code furas. I want to display different data frames each time when the user selects Dataframe name on options menu..Dataframes are stored in dictionary.But when I used the above code for my purpose , I am getting name error:NameError: name 'df2' is not defined – Kavikayal Sep 10 '20 at 12:00
  • better create question on new page and add all information and minimal working code with your problem. Are you use global variable `df2` ? Did you define `df2` at start with some value? – furas Sep 10 '20 at 12:34