Use pandas and pandastable to insert value into selected cell

Viewed 1046

I have a pandastable that displays in my tkinter GUI.

Using the value from the tkinter option menu, value = Status.get() I would like to select a particular cell in my pandastable and use the value from value = Status.get() to append the cell.

I know how I can append a value to a dataframe using set_value() within a function but Im not sure on how I can return the position of the selected cell using pandastable? Or if its possible?

I have played with tkinter treeview widget and you can return selected cell position with .focus()

I have been referencing methods using the doc: https://pandastable.readthedocs.io/en/latest/_modules/pandastable/core.html#Table.get_col_clicked

But I cant find anything to fit my purpose...

See below the screenshot of my pandastable the Column Current Status row 2 position has been double clicked. I want to return this position.

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 screenshot

1 Answers

You can use the following function to update the selected cell:

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

where app is the instance of TestApp created inside select_input_file() function. You need to declare it global inside the function:

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

Also you need to find a way to call the set_cell() function, the easy way is to use button.


Update: If you use the command option of OptionMenu, like below:

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

You need to change the set_cell() function as below:

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