Reputation: 349
I am currently working on a GUI that displays pandas dataframes.
I want an OptionMenu
to select the dataframe from a list and then display it in the pandastable.
My current approach: I have an attribute curTable in my class and I initially fill it with the first dataframe, which is correctly displayed in the pandastable. Then when the value of my StringVar
connected to my OptionMenu
changes I call a callback function that changes the attribute and redraw the pandastable.
import tkinter as tk
from pandastable import Table, TableModel
import pandas as pd
class test_gui():
def __init__(self):
self.root = tk.Tk()
LF3 = tk.LabelFrame(self.root, text='Output')
LF3.grid(column=2, row=0, padx=(4,4), pady=4, sticky="nsew")
def tableSelChange(*args):
df1 = pd.DataFrame([[1,2,3],['what', 'is', 'this'],[123, 123, 123]],
columns=['This', 'is', 'a Test'])
df2 = pd.DataFrame([['bruh', 'this', 'is a gui'],[1235, 1235, 1235],[123, 123, 123]],
columns=['This', 'is', 'a Test2'])
if tableSelVar.get() == 'One':
print('ONE')
self.curTable = df1
elif tableSelVar.get() == 'Two':
print('Two')
self.curTable = df2
#self.dfTbl.redraw()
#dfTbl.tableChanged()
dfTbl.redraw()
tableSelVar = tk.StringVar(LF3)
tableSelVar.set('No tables available')
tableSelMenu = tk.OptionMenu(LF3, tableSelVar, 'One', 'Two')
tableSelMenu.pack()
df1 = pd.DataFrame([[1,2,3],['what', 'is', 'this'],[123, 123, 123]],
columns=['This', 'is', 'a Test'])
df2 = pd.DataFrame([['bruh', 'this', 'is a gui'],[1235, 1235, 1235],[123, 123, 123]],
columns=['This', 'is', 'a Test2'])
self.curTable = df1
LLF31 = tk.LabelFrame(LF3, text='Table editor')
LLF31.pack()
dfTbl = Table(LLF31, dataframe=self.curTable, showtoolbar=True, showstatusbar=True)
dfTbl.show()
tableSelVar.trace('w', tableSelChange)
self.root.mainloop()
if __name__ == '__main__':
t = test_gui()
When I select the second dataframe, the pandastable does not change. How can I achieve this?
Upvotes: 2
Views: 5076
Reputation: 349
Ok, I think I have the solution:
I am using TableModel to load in the dataframe and then use the method updateModel()
of pandastable.
def tableSelChange(*args):
df1 = pd.DataFrame([[1,2,3],['what', 'is', 'this'],[123, 123, 123]],
columns=['This', 'is', 'a Test'])
df2 = pd.DataFrame([['bruh', 'this', 'is a gui'],[1235, 1235, 1235],[123, 123, 123]],
columns=['This', 'is', 'a Test2'])
if tableSelVar.get() == 'One':
print('ONE')
dfTbl.updateModel(TableModel(df1))
elif tableSelVar.get() == 'Two':
print('Two')
dfTbl.updateModel(TableModel(df2))
dfTbl.redraw()
Upvotes: 4