[英]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 in my terminal , I want to see the subset dataframe after selecting into dropdown in my GUI , Please let me know how to see the subset dataframe a/c to dropdown option into my GUI .我在 tkinter 中有下拉选项,它通过数据帧 Pandas 通过 col1 选择分组下拉选项,现在我可以通过单击终端中的确定按钮来查看数据帧的子集,我想在选择下拉列表后查看子集数据帧我的 GUI,请让我知道如何在我的 GUI 中看到子集数据框 a/c 到下拉选项。
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)
def showdata():
row, column = df2.shape
for r in range(row):
for c in range(column):
e1 = tk.Entry(Frame1)
e1.insert(1, df2.iloc[r, c])
e1.grid(row=r, column=c, padx=2, pady=2)
e1.config(state='disabled')
# print(df.groupby(''))
# exit()
Exitbutton = tk.Button(Frame1, text="EXIT", fg="red", bd=5, width=3, height=2, command=root.quit)
Exitbutton.pack()
#Exitbutton.grid(row=41, column=2)
nextbutton = tk.Button(Frame1, text="Next Data", fg="red", bd=5, width=7, height=2,command=showdata)
nextbutton.pack()
#nextbutton.grid(row=41, column=3)
# --- 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()
Frame1=tk.Frame(root,bd=5)
values = ['all'] + list(df['TIME'].unique())
selected = tk.StringVar()
options = tk.OptionMenu(Frame1, selected, *values)
options.pack()
button = tk.Button(Frame1, text='OK', command=on_click)
button.pack()
button2 = tk.Button(Frame1, text='OK', command=on_click)
button.pack()
root.mainloop()
I am getting blank Tkinter Window , without using showdata function i am getting dropdown option and data are showing in terminal but i want the subset dataframe to display on GUI for that i created showdata() but it is not working.我得到了空白的 Tkinter 窗口,没有使用 showdata 函数,我得到下拉选项,数据显示在终端中,但我希望子集数据框显示在 GUI 上,因为我创建了 showdata() 但它不起作用。 Kindly let me know how to solve this issue , I will be appreciable请让我知道如何解决这个问题,我将不胜感激
** For more details on dropdown options show you can go below link enter link description here ** 有关下拉选项的更多详细信息,请显示您可以转到以下链接, 在此处输入链接说明
It is my last code from previous question这是我上一个问题的最后一个代码
EDIT: I added command=
to OptionMenu
so now it doesn't need Button
to accept selection.编辑:我将command=
添加到OptionMenu
所以现在它不需要Button
来接受选择。
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)
def on_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编辑: pandastable 的示例较短,并且在鼠标右键单击时具有内置功能 - 就像排序
import tkinter as tk
import pandas as pd
from pandastable import Table
# --- functions ---
def on_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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.