Subset Dataframe To Show On Gui Tkinter
I have dropdown option in tkinter which select the option of dropdown by groupby the col1 by dataframe pandas , Now I am able to see the subset of dataframe by clicking ok button i
Solution 1:
It is my last code from previous question
EDIT: I added command=
to OptionMenu
so now it doesn't need Button
to accept selection.
import tkinter as tk
import pandas as pd
# --- functions ---defshowdata():
global table
# destroy old frame with tableif 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 inrange(row):
for c inrange(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')
defon_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)
defon_select(val):
global df2
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, command=on_select)
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()
EDIT: Example with pandastable is shorter and it has built-in function in mouse right click - like sorting
import tkinter as tk
import pandas as pd
from pandastable import Table
# --- functions ---defon_select(val):
if val == 'all':
pt.model.df = df
else:
pt.model.df = df[ df['TIME'] == val ]
# refresh/redraw table in window
pt.redraw()
# --- 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()
# create frame for pandas table
table_frame = tk.Frame(root)
table_frame.pack()
# add pandastable do frame
pt = Table(table_frame, dataframe=df) # it can't be `root`, it has to be `frame`
pt.show()
pt.setRowColors(cols=[2], rows=[2, 3], clr='green')
values = ['all'] + list(df['TIME'].unique())
selected = tk.StringVar()
options = tk.OptionMenu(root, selected, *values, command=on_select)
options.pack()
root.mainloop()
Post a Comment for "Subset Dataframe To Show On Gui Tkinter"