将值添加到 treeview Tkinter 中的特定列 [英] Adding values to specific column in treeview Tkinter
问题描述
我制作了一个树视图,我想在第一列和第二列中添加值,然后程序需要计算当您按下 ENTER 按钮时将放入第三列的值(我使用了事件和绑定).我不知道如何将值放在 treeview 的特定列中,我总是收到此错误:
I made a treeview, and I want to add values in first and second column and then the program needs to calculate the values that will put in third column when you press the ENTER button (I used events and binds). I do not know how to put values in the specific column in treeview , I always get this error:
TypeError: 'float' object is not subscriptable
我知道如何一次在每一列中插入值,但我不知道如何只向特定列插入一个值,而不更改其他列中的值.这是我写的函数:
I know how to insert values in every column at once, but I do not know how to insert only one value to a specific column, without changing the values in other columns. This is the function that I wrote:
def PlannedCosPerSize(event):
try:
for child in tree.get_children():
Size=round(float(tree.item(child,"values")[1]),2)
PlannedCost=round(float(tree.item(child,"values")[2]),2)
PlanCostPerSize=float(round(PlannedCost/Size,2))
tree.insert("","end", values=(PlanCostPerSize)[4])
print(PlanCostPerSize)
except IndexError:
Error=messagebox.showinfo("error","You have error")
pass
tree.bind('<Return>', PlannedCosPerSize) # validate with Enter
推荐答案
insert
方法正在Treeview
中创建一个新项目,但您要做的是编辑一个现有的项目,所以这不是使用的方法.
The insert
method is creating a new item in the Treeview
but what you want to do is editing an existing item, so this is not the method to use.
一种可能性是使用 Treeview
的 set
方法来获取或设置特定列中的值:
One possibility is to use the set
method of the Treeview
to either get or set the value in a specific column:
treeview.set(item, '#1')
将为您提供第一列中的值.treeview.set(item, '#3', new_value)
会将第三列的值更改为new_value
.
treeview.set(item, '#1')
will give you the value in the first column.treeview.set(item, '#3', new_value)
will change the value of the third column intonew_value
.
您还可以使用列的名称(在创建 Treeview
时在 columns=
中给出的名称)而不是 '#
You can also use the column's name (the one given in columns=
when creating the Treeview
) instead of '#<column number>'
.
另一种可能是使用item
方法:
Another possibility is to use the item
method:
old_values = treeview.item(item, 'values')
将为您提供项目的值.treeview.item(item, values=(old_values[0], old_values[1], new_value))
将更改项目的最后一个值.
old_values = treeview.item(item, 'values')
will give you the values of the item.treeview.item(item, values=(old_values[0], old_values[1], new_value))
will change the last value of the item.
Treeview 小部件的文档:https:///anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-Treeview.html
Documentation on the Treeview widget: https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-Treeview.html
这篇关于将值添加到 treeview Tkinter 中的特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!