【问题标题】:DropDown option to show subset of dataframe Tkinter显示数据帧 Tkinter 子集的下拉选项
【发布时间】:2020-05-10 10:06:33
【问题描述】:

我有一个数据框 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

【问题讨论】:

  • 使用选项创建下拉列表,从下拉列表中获取值并在 pandas 中使用它来选择要显示的行,然后显示它们。见:OptionMenu
  • 任何类似的例子?如何在下拉列表中将行和列作为变量传递,我有 6 列的 400 多行。
  • 在链接中有如何使用 OptionMenu 的示例。只需从 OptionMenu 中获取值并将其与 pandas 一起使用 - df2 = df1[ filter ] - 然后显示 df2。坦率地说,我不明白你想选择什么。如果您想要更好的示例,那么首先创建我们可以运行和修改的最小工作代码。

标签: python pandas tkinter dropdown


【解决方案1】:

如何使用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()

【讨论】:

  • 非常感谢,非常感谢您的帮助,但我希望在下拉列表中选择时间 00 时显示所有行和列数据,然后显示 00 时间的所有行和列数据,请让我知道如何进行,00 时间的行和列可以在输入或标签中。谢谢。
  • 但我的例子是这样做的。当您选择时间01:00 时,它会显示(print())所有有时间的行和列01:00
  • 实际上我想在 GUI 上而不是在终端上查看数据,因为我的尝试低于在 Entry _tkinter.TclError: bad option "-row": must be - after、-anchor、-before、-expand、-fill、-in、-ipadx、-ipady、-padx、-pady 或 -side 使用 Entry
  • 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')
  • 正如我在回答中所写:在 GUI 中显示是不同的大问题。
猜你喜欢
  • 2017-03-25
  • 1970-01-01
  • 2021-02-16
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多