Python 更改PandaTable中显示的数据帧(tkinter小部件)
我目前正在开发一个显示数据帧的GUI 我需要一个Python 更改PandaTable中显示的数据帧(tkinter小部件),python,python-3.x,pandas,tkinter,Python,Python 3.x,Pandas,Tkinter,我目前正在开发一个显示数据帧的GUI 我需要一个选项菜单从列表中选择数据帧,然后将其显示在PandaTable中 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.Label
选项菜单
从列表中选择数据帧,然后将其显示在PandaTable中
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()
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()
我目前的方法是:在我的类中有一个属性curTable,我首先用第一个数据帧填充它,它正确地显示在pandastable中。然后,当连接到myOptionMenu
的myStringVar
的值更改时,我调用一个回调函数,该函数更改属性并重新绘制PandaTable
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()
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()
当我选择第二个数据帧时,pandastable不会改变。我如何才能做到这一点?好的,我想我有了解决方案: 我使用TableModel加载数据帧,然后使用PandaTable的方法
updateModel()
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()
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()