DropDown选项显示数据帧Tkinter的子集

DropDown选项显示数据帧Tkinter的子集

本文介绍了DropDown选项显示数据帧Tkinter的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框熊猫,我想让GUI显示数据,我有date_time一列,它每隔一小时显示一次数据。我想创建一个下拉选项,假设用户选择1小时,然后仅选择1小时所有列行显示,如果用户选择2小时,则第二列&行显示。 Cany任何人都可以帮助我如何显示数据赋值下拉选项。
我会很感激的。

I have a dataframe pandas of which i want to make GUI to display the data , i have date_time one column which show data every one hour interval i want to make a dropdown option suppose if user select 1 hours then only the 1 hours all columns & rows show , if user select 2 hour then second all columns & rows display . Cany any one please help me how to display the data gving dropdown option.I will really Appreciate it. Thanks in Advance.

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

Minimal example how to use OptionMenu to filter data in DataFrame

我不会尝试在tkinter中显示它,因为它是另一个问题。

I will not try to display it in tkinter because it is different problem.

我创建的列表的值将显示在 OptionMenu

I create list with values which I will display in OptionMenu

values = ['all'] + list(df['TIME'].unique())

和在按下按钮后将用于选择的变量

and variable which I will use to get selection after pressing button

selected = tk.StringVar()

当我得到选择,然后我就可以使用

When I get selection then I can use

df2 = df[ filter ]

仅选择一些行。

如果需要某些内容与之不同,则您必须更好地描述问题并显示一些可用于测试和修改的代码。

If you need something different then you have to better describe problem and show some code which can be used to test and modifications.

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 -不理想,但它显示所有或已过滤的数据。


version with data displayed in GUI - not ideal but it shows all or filtered data.

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()

这篇关于DropDown选项显示数据帧Tkinter的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:08