我有一个数据框熊猫,我想让GUI显示数据,我有date_time一列,它每隔一小时显示一次数据。我想创建一个下拉选项,假设用户选择1小时,则仅选择1小时显示列和行,如果用户选择2小时,则显示所有列和行。 Cany任何人都可以帮助我如何显示数据赋值下拉选项。我会很感激的。在此先感谢。
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
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()
当我遇到错误时,您可以更正吗?+ self._options(cnf,kw))_tkinter.TclError:无法在内部使用几何图形管理器网格。已经有包管理的奴隶
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(root)
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(root, text="EXIT", fg="red", bd=5, width=3, height=2, command=root.quit)
Exitbutton.pack()
#Exitbutton.grid(row=41, column=2)
nextbutton = tk.Button(root, 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()
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()
button2 = tk.Button(root, text='OK', command=on_click)
button.pack()
root.mainloop()