首页 > 解决方案 > 使用 pandas 和 pandastable 将值插入选定的单元格

问题描述

我有一个pandastable显示在我的tkinterGUI 中。

使用tkinter选项菜单中的值,value = Status.get()我想在 my 中选择一个特定的单元格pandastable并使用来自的值value = Status.get()来附加单元格。

我知道如何将值附加到函数中的dataframeusingset_value()但我不确定如何使用返回所选单元格的位置pandastable?或者如果可能?

我玩过tkinter treeview小部件,您可以返回选定的单元格位置.focus()

我一直在使用文档引用方法:https ://pandastable.readthedocs.io/en/latest/_modules/pandastable/core.html#Table.get_col_clicked

但我找不到任何适合我的目的...

请参阅下面我pandastableColumn Current Status row 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()

pandastable 截图

标签: pythonpandastkinter

解决方案


您可以使用以下函数来更新选定的单元格:

def set_cell():
    row = app.table.getSelectedRow()
    col = app.table.getSelectedColumn()
    app.table.model.setValueAt(Status.get(), row, col)
    app.table.redraw()

created inside函数app的实例在哪里。您需要在函数内将其声明为全局:TestAppselect_input_file()

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_cell()函数的方法,最简单的方法是使用按钮。


更新:如果您使用 的command选项OptionMenu,如下所示:

drop=tk.OptionMenu(root, Status, "Select Status", *StatusList, command=set_cell)

您需要更改set_cell()功能如下:

def set_cell(val):
    row = app.table.getSelectedRow()
    col = app.table.getSelectedColumn()
    app.table.model.setValueAt(val, row, col)
    app.table.redraw()

推荐阅读