1

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

MSA msa
  • 83
  • 1
  • 10
  • `Frame1.pack()` to show frame. – furas Jan 24 '20 at 12:03
  • use `Frame1` to put only `Entry` using `grid()`. Other elements put directly in `root` using `pack()` – furas Jan 24 '20 at 12:06
  • I added my last code from previous question and I added method to change table after selectiong option without pressing code. I also working on small example with [pandastable](https://github.com/dmnfarrell/pandastable) which seems more useful then manuall creating table. – furas Jan 24 '20 at 13:01
  • Yes , Pandastable is very easy and useful , i think i should try pandastable also for visualization data into it . By Reading the Documenatation of Pandastable isn''t it i will be able to learn the Pandastable or any other way to learn pandastable you can guide ? – MSA msa Jan 24 '20 at 13:08
  • I made example with pandastable in answer below. There are some [basic examples](https://pandastable.readthedocs.io/en/latest/examples.html) in doc but there is also full working tool [DataExplore](https://pandastable.readthedocs.io/en/latest/dataexplore.html) and its souce code can be useful as example. – furas Jan 24 '20 at 13:17

1 Answers1

1

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

enter image description here

enter image description here

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()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you alot for helping me, I want to add one more feature in col B where ever x is there there x color become green, y then y cell color red and z then cell color blue , I think we can do by table.colorbyvalue() but i am not able to findout the example of it ,Any link for colorbyvalue() ? – MSA msa Jan 27 '20 at 06:05
  • I founld only [Table Coloring](https://github.com/dmnfarrell/pandastable/wiki/Table-Coloring) and source code for [setRowColors](https://pandastable.readthedocs.io/en/latest/_modules/pandastable/core.html#Table.setRowColors) which can set color for every cell separatelly `pt.setRowColors(cols=[2], rows=[2, 3], clr='green')` – furas Jan 27 '20 at 10:30
  • create new question on new page and more people will see it and there will be place for new answer. I found similar question [Set colors in pandastable](https://stackoverflow.com/questions/58448763/set-colors-in-pandastable/) and I made answer with example. – furas Jan 27 '20 at 14:30
  • By using pt.setRowColors(cols=[2], rows=[2, 3], clr='green') I am getting an Error pt.setRowColors(cols=[2], rows=[2, 3], clr='green') AttributeError: 'Table' object has no attribute 'setRowColors' Even I have used pt.columncolors['B'] = '#dcf1fc' , same Error is coming – MSA msa Jan 28 '20 at 03:26
  • when I use `pt.setRowColors(cols=[2], rows=[2, 3], clr='green')` in my last code in answer then it changes color. Maybe your object `Table` is not from `pandastable`. BTW: I added `pt.setRowColors(cols=[2], rows=[2, 3], clr='green')` to code in example but you should creae new question on new page and you will have place to show code which you used and full error message which you get. – furas Jan 28 '20 at 08:06
  • same code if i am running then i am getting error pt.setRowColors(cols=[2], rows=[2, 3], clr='green') AttributeError: 'Table' object has no attribute 'setRowColors' – MSA msa Feb 05 '20 at 04:41
  • create new question on new page - you will have space to show full code which makes this problem. It seems your `Table` is not `pandastable.Table` – furas Feb 05 '20 at 08:13
  • I created new question , link https://stackoverflow.com/questions/60077046/set-color-of-cell-of-one-column-based-on-value-of-column-cell-example-pandastabl i want b column cell value color to change , if x then green & if y value then red , Thank you. – MSA msa Feb 05 '20 at 13:35