1

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

James Cook
  • 225
  • 1
  • 11
  • 2
    This question [set-value-for-particular-cell-in-pandas-dataframe-using-index](https://stackoverflow.com/questions/13842088/set-value-for-particular-cell-in-pandas-dataframe-using-index) may help. – acw1668 Aug 24 '20 at 11:42
  • Very useful thread, great info. I cant find anything in there for this particular purpose? Ive updated question with a screenshot. Hope it tells a better story.. Thanks again – James Cook Aug 24 '20 at 21:48

1 Answers1

1

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()
acw1668
  • 19,579
  • 2
  • 14
  • 28
  • Thank you for your descriptive answer, but I am getting error `AttributeError: module 'pandastable.app' has no attribute 'table'` using `drop=tk.OptionMenu(root, Status, "Select Status", *StatusList, command = set_cell())` – James Cook Aug 25 '20 at 06:49
  • 1
    @JamesCook The callback of `command` option of `OptionMenu` will be passed an argument, the selected item. See my update answer. – acw1668 Aug 25 '20 at 06:49
  • Sorry I should have picked up on that... Thank you, very much. Again, a great help – James Cook Aug 25 '20 at 06:54