要在 GUI Tkinter 上显示的子集数据框 [英] subset dataframe to show on GUI Tkinter

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

问题描述

我在 tkinter 中有下拉选项,它通过数据帧 Pandas 通过 col1 通过 groupby 选择下拉选项,现在我可以通过单击终端中的确定按钮来查看数据帧的子集,我想在选择后查看子集数据帧进入我的 GUI 中的下拉菜单,请让我知道如何在我的 GUI 中看到子集数据框 a/c 到下拉选项.

导入 tkinter 作为 tk将熊猫导入为 pd#  - - 职能  - -定义 on_click():val = selected.get()如果 val == 'all':打印(df)别的:df2 = df[ df['TIME'] == val ]打印(df2)定义显示数据():行,列 = df2.shape对于范围(行)中的 r:对于范围(列)中的 c: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')# 打印(df.groupby(''))# 出口()Exitbutton = tk.Button(Frame1, text="EXIT", fg="red", bd=5, width=3, height=2, command=root.quit)退出按钮.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)#  - - 主要的  - -df = pd.DataFrame({'时间': ['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'],})根 = tk.Tk()Frame1=tk.Frame(root,bd=5)values = ['all'] + list(df['TIME'].unique())选择 = tk.StringVar()options = tk.OptionMenu(Frame1, selected, *values)options.pack()button = tk.Button(Frame1, text='OK', command=on_click)按钮.pack()button2 = tk.Button(Frame1, text='OK', command=on_click)按钮.pack()root.mainloop()

我得到空白的 Tkinter 窗口,没有使用 showdata 函数我得到下拉选项和数据显示在终端中,但我希望子集数据框显示在 GUI 上,因为我创建了 showdata() 但它不起作用.请让我知道如何解决这个问题,我会很感激

** 有关下拉选项的更多详细信息,您可以转到以下链接

导入 tkinter 作为 tk将熊猫导入为 pd从 pandastable 导入表#  - - 职能  - -def on_select(val):如果 val == 'all':pt.model.df = df别的:pt.model.df = df[ df['TIME'] == val ]# 在窗口中刷新/重绘表格pt.redraw()#  - - 主要的  - -df = pd.DataFrame({'时间': ['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'],})根 = tk.Tk()# 为熊猫表创建框架table_frame = tk.Frame(root)table_frame.pack()# 添加pandastable do 框架pt = Table(table_frame, dataframe=df) # 不能是`root`,必须是`frame`pt.show()pt.setRowColors(cols=[2], rows=[2, 3], clr='green')values = ['all'] + list(df['TIME'].unique())选择 = tk.StringVar()options = tk.OptionMenu(root, selected, *values, command=on_select)options.pack()root.mainloop()

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 .

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. 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.

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

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

这篇关于要在 GUI Tkinter 上显示的子集数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆