Python 使用pandas和pandastable将值插入选定单元格
我有一个显示在我的Python 使用pandas和pandastable将值插入选定单元格,python,pandas,tkinter,Python,Pandas,Tkinter,我有一个显示在我的tkinterGUI中的pandastable 使用tkinter选项菜单中的值,value=Status.get() 我知道如何在函数中使用set_value()将值附加到dataframe,但我不确定如何使用pandastable返回所选单元格的位置?或者如果可能的话 我已经使用了tkinter treeview小部件,您可以使用.focus() 我一直在使用文档引用方法: 但是我找不到任何适合我的目标 请参见下面我的pandastable屏幕截图,双击了列当前状态第2行位
tkinter
GUI中的pandastable
使用tkinter
选项菜单中的值,value=Status.get()
我知道如何在函数中使用set_value()
将值附加到dataframe
,但我不确定如何使用pandastable
返回所选单元格的位置?或者如果可能的话
我已经使用了tkinter treeview
小部件,您可以使用.focus()
我一直在使用文档引用方法:
但是我找不到任何适合我的目标
请参见下面我的pandastable
屏幕截图,双击了列当前状态
第2行
位置。我想返回这个职位
root = tk.Tk()
root.geometry("2000x1000")
root.title('Workshop Manager')
def select_input_file():
global df, app
input_file_path = filedialog.askopenfilename(filetypes=(("CSV files", "*.csv"),))
app = TestApp(root, input_file_path)
app.place(bordermode=INSIDE, height=500, width=2000, x=0, y=50)
df = app.table.model.df
class TestApp(tk.Frame):
def __init__(self, parent, input_file_path, editable = True, enable_menus = False):
super().__init__(parent)
self.table = Table(self, showtoolbar=False, showstatusbar=False)
self.table.importCSV(input_file_path)
self.table.show(input_file_path)
self.table.addColumn('Current Status')
self.table.addColumn('Assign Technician')
self.table.autoResizeColumns()
def set_cell():
row = app.table.getSelectedRow()
col = app.table.getSelectedColumn()
app.table.model.setValueAt(Status.get(), row, col)
app.table.redraw()
StatusList = [
"Carry Over",
"Waiting On Parts",
"Waiting On Car Wash",
"Yet to Arrive",
"Not Started",
"Being Worked On",
]
Status = StringVar()
Status.set(0)
drop=tk.OptionMenu(root, Status, "Select Status", *StatusList, command = set_cell())
drop.place(x=1440, y=0, height=50, width=150)
root.mainloop()
您可以使用以下功能更新所选单元格:
def set_cell():
row = app.table.getSelectedRow()
col = app.table.getSelectedColumn()
app.table.model.setValueAt(Status.get(), row, col)
app.table.redraw()
其中app
是在select\u input\u file()
函数中创建的TestApp
的实例。您需要在函数中声明它为全局:
def select_input_file():
global df, app
input_file_path = filedialog.askopenfilename(filetypes=(("CSV files", "*.csv"),))
app = TestApp(root, input_file_path)
app.place(bordermode=INSIDE, height=500, width=2000, x=0, y=50)
df = app.table.model.df
您还需要找到一种方法来调用set\u cell()
函数,简单的方法是使用按钮
更新:如果使用选项菜单
的命令
选项,如下所示:
drop=tk.OptionMenu(root, Status, "Select Status", *StatusList, command=set_cell)
您需要更改set\u cell()
函数,如下所示:
def set_cell(val):
row = app.table.getSelectedRow()
col = app.table.getSelectedColumn()
app.table.model.setValueAt(val, row, col)
app.table.redraw()
这个问题可能会有帮助。非常有用的线索,很好的信息。我在里面找不到有这个特殊用途的东西?我用截图更新了问题。希望它能讲述一个更好的故事。。再次感谢您提供的描述性回答,但我发现错误:模块“pandastable.app”没有属性“table”
使用drop=tk.OptionMenu(根,状态,“选择状态”,“状态列表”,“命令=设置单元格())
@JamesCook将向选项菜单
的命令
选项的回调传递一个参数,即所选项目。查看我的更新答案。很抱歉,我本该知道。。。多谢各位。这也是一个很大的帮助