python - 使用 pandas 和 pandastable 将值插入选定的单元格
问题描述
我有一个pandastable
显示在我的tkinter
GUI 中。
使用tkinter
选项菜单中的值,value = Status.get()
我想在 my 中选择一个特定的单元格pandastable
并使用来自的值value = Status.get()
来附加单元格。
我知道如何将值附加到函数中的dataframe
usingset_value()
但我不确定如何使用返回所选单元格的位置pandastable
?或者如果可能?
我玩过tkinter treeview
小部件,您可以返回选定的单元格位置.focus()
我一直在使用文档引用方法:https ://pandastable.readthedocs.io/en/latest/_modules/pandastable/core.html#Table.get_col_clicked
但我找不到任何适合我的目的...
请参阅下面我pandastable
的Column 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()
解决方案
您可以使用以下函数来更新选定的单元格:
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
的实例在哪里。您需要在函数内将其声明为全局:TestApp
select_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()
推荐阅读
- c# - Web API - 模型绑定错误消息
- javascript - 如何打开“调试器”;离开
- css - 居中 WordPress 标题
- excel - 对象不支持此属性或方法 - 如何将其他操作放入此循环?
- ffmpeg - 添加 GIF 作为水印和循环 FFMPEG
- html - HTML Table -how to put multiple elements into a ?
- java - TreeMap 按值错误排序
- reactjs - Typescript/React 中的样式输入错误
- javascript - RSpec/Capybara javascript测试引发驱动程序错误
- python - 优化:如何改进 if 语句?