Python 在 TreeView Tkinter 中删除和编辑项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32511843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete and Edit items in TreeView Tkinter
提问by J. Dru
I want to delete a single row in a TreeView
in Tkinter.
我想TreeView
在 Tkinter 中删除 a 中的一行。
I know that this method:
我知道这个方法:
def delButton(self):
x = main.tree.get_children()
for item in x:
main.tree.delete(item)
deletes the whole tree. But I want to delete only one row. How can I do this?
删除整棵树。但我只想删除一行。我怎样才能做到这一点?
Moreover, I want to know how to edit a TreeView
row as well.
此外,我还想知道如何编辑TreeView
一行。
采纳答案by VRage
You are not deleting the whole tree you are just deleting all children from the root item, because you use delete for each item in your iteration.
You can use a if
statement to determine which item you want, or you can get the selected item with selected_item = tree.selection()[0]
and delete it. With the .item()
method you can full access to the item for modification. Example:
您不是删除整个树,而是从根项目中删除所有子项,因为您对迭代中的每个项目都使用了删除。您可以使用if
语句来确定您想要哪个项目,或者您可以获取所选项目selected_item = tree.selection()[0]
并将其删除。使用该.item()
方法,您可以完全访问要修改的项目。例子:
from Tkinter import Tk, Button
import ttk
root = Tk()
tree = ttk.Treeview(root)
tree["columns"]=("one","two")
tree.column("one", width=100 )
tree.column("two", width=100)
tree.heading("one", text="coulmn A")
tree.heading("two", text="column B")
tree.insert("" , 0, text="Line 1", values=("1A","1b"))
id2 = tree.insert("", 1, "dir2", text="Dir 2")
tree.insert(id2, "end", "dir 2", text="sub dir 2", values=("2A","2B"))
##alternatively:
tree.insert("", 3, "dir3", text="Dir 3")
tree.insert("dir3", 3, text=" sub dir 3",values=("3A"," 3B"))
def edit():
x = tree.get_children()
for item in x: ## Changing all children from root item
tree.item(item, text="blub", values=("foo", "bar"))
def delete():
selected_item = tree.selection()[0] ## get selected item
tree.delete(selected_item)
tree.pack()
button_del = Button(root, text="del", command=delete)
button_del.pack()
button_del = Button(root, text="edit", command=edit)
button_del.pack()
root.mainloop()
回答by scotty3785
Try something like this.
尝试这样的事情。
def delete(event):
print('delete')
selected_item = tree1.selection()[0]
values = tuple(tree1.item(selected_item)['values'])
print(dir(selected_item))
print(selected_item)
print(values)
conn2 = sq.connect('Clients.db')
c2 = conn2.cursor()
query = "DELETE FROM clients WHERE name=? AND phone=?"
c2.execute(query,(*values))
conn2.commit()
tree1.delete(selected_item)
We need to get the values associated with the selected item which is what the tree1.item(selected_item)['values']
section does.
我们需要获取与所选项目相关联的值,这就是该tree1.item(selected_item)['values']
部分的作用。
Will need some modifications since you didn't provide a complete example of your code so I don't know what values are entered in to the treeview.
由于您没有提供完整的代码示例,因此需要进行一些修改,因此我不知道在树视图中输入了哪些值。
回答by Andrs Armando Jaramillo Montao
def delete_records(self):
selection=self.tree.selection()[0]
self.tree.delete(selection)