python获取treeview中的值
时间: 2023-04-13 08:03:26 浏览: 1283
无固定节点的TreeView的遍历实例
5星 · 资源好评率100%
Python可以使用treeview的get_children()方法获取treeview中所有子项的ID,然后使用item()方法获取每个子项的值。例如:
```python
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# 创建treeview
tree = ttk.Treeview(root)
tree.pack()
# 添加子项
tree.insert("", "end", "item1", text="Item 1")
tree.insert("", "end", "item2", text="Item 2")
tree.insert("item1", "end", "subitem1", text="Subitem 1")
tree.insert("item1", "end", "subitem2", text="Subitem 2")
# 获取treeview中的值
for child_id in tree.get_children():
child_value = tree.item(child_id)["text"]
print(child_value)
root.mainloop()
```
输出结果为:
```
Item 1
Item 2
Subitem 1
Subitem 2
```
阅读全文