Matix
Matix

Reputation: 128

Python Tkinter, How do I get the subitems of an Item in Tkinter TreeView?

how can I get the Subitems of an item in a Tkinter TreeView?

-Item
|-Subitem1
||-Subsubitem1  \ This two items
||-Subsubitem2  /
|-Subitem2
|-Subitem3

Thanks

Upvotes: 0

Views: 1303

Answers (1)

Henry Yik
Henry Yik

Reputation: 22493

The command is tree.get_children(item=None). You can call it recursively to go through the child nodes.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
tree = ttk.Treeview(root)

a = tree.insert("", "end", text="Item",open=True)
b = tree.insert(a, "end", text="Subitem1",open=True)
tree.insert(a, "end", text="Subitem2")

for i in range(1,3):
    tree.insert(b, "end", text=f"Subsubitem{i}")

tree.pack()

def get_child():
    for item in tree.get_children():
        for subitem in tree.get_children(item):
            for subsubitem in tree.get_children(subitem):
                print (subsubitem, tree.item(subsubitem)["text"])

tk.Button(root,text="click me",command=get_child).pack()

root.mainloop()

Upvotes: 1

Related Questions