【问题标题】:Use pandas and pandastable to insert value into selected cell使用 pandas 和 pandastable 将值插入选定的单元格
【发布时间】:2020-12-13 00:56:09
【问题描述】:

我有一个pandastable,它显示在我的tkinter GUI 中。

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

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

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

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

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

看下面我的pandastable截图,Column Current Statusrow 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()

【问题讨论】:

标签: python pandas tkinter


【解决方案1】:

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

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

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


更新:如果您使用OptionMenucommand 选项,如下所示:

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()

【讨论】:

  • 感谢您的描述性回答,但我收到错误 AttributeError: module 'pandastable.app' has no attribute 'table' 使用 drop=tk.OptionMenu(root, Status, "Select Status", *StatusList, command = set_cell())
  • @JamesCook OptionMenucommand 选项的回调将传递一个参数,即所选项目。请参阅我的更新答案。
  • 对不起,我应该知道...非常感谢。再次,一个很大的帮助
猜你喜欢
  • 1970-01-01
  • 2019-01-24
  • 2014-12-16
  • 2022-01-20
  • 2021-12-17
  • 2022-12-18
相关资源
最近更新 更多