简体   繁体   中英

subset dataframe to show on GUI Tkinter

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM