python - 显示数据帧 Tkinter 子集的下拉选项
问题描述
我有一个数据框 pandas,我想制作 GUI 来显示数据,我有 date_time 一列,每隔一小时显示一次数据我想做一个下拉选项假设如果用户选择 1 小时然后只有 1 小时所有列&行显示,如果用户选择 2 小时,则显示所有列和行。任何人都可以帮助我如何显示数据提供下拉选项。我真的很感激。提前致谢。
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
解决方案
最小示例如何使用 OptionMenu 过滤数据DataFrame
我不会尝试在 tkinter 中显示它,因为它是不同的问题。
我用我将在其中显示的值创建列表OptionMenu
values = ['all'] + list(df['TIME'].unique())
和变量,我将在按下按钮后用来获取选择
selected = tk.StringVar()
当我得到选择时,我可以使用
df2 = df[ filter ]
只选择一些行。
如果您需要不同的东西,那么您必须更好地描述问题并显示一些可用于测试和修改的代码。
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()
编辑:显示数据的版本GUI
- 不理想,但它显示所有或过滤的数据。
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()
推荐阅读
- python - 如何使用 python 和 matplotlib 为矩形内的颜色填充设置动画?
- azure - 我们如何在 Azure 应用程序洞察中显示数据库查询
- sql - 插入包含基于同一行的计算列的行
- mongodb - 测试容器中的 Mongo
- c# - 如何在 C# 中多次将内容从一个数据表复制到另一个数据表
- django - 没有在 django 项目上安装 requirements.txt
- ios - UIAccessibilityCustomAction 选择器不适用于静态方法
- scala - 几天后,Kafka Streams 变慢/不读取数据
- c++ - What exactly is the 'vertexPosition_modelspace' attribute
- jquery - 如何将表情符号面板添加到 Extjs 项目