Is there a tkinter command to extract the item ID (iid) of all detached items in a ttk.Treeview
widget?
2 Answers
There's no way to enumerate the detached items in the Treeview. You can enumerate the attached children of the root node, but that doesn't include the detached children by definition. You also can't find out whether a particular known item is attached (directly or indirectly) to the root (but might be able to infer it by a combination of the parent
and children
methods; detached items both have an empty parent and aren't listed in the children of the root node).
In short, once you lose the item ID of a detached item, you've effectively leaked the item (until you destroy the widget, so it's not formally a memory leak).
This needs a feature request to fix. It's an API omission. (I've opened a branch with an implementation.)
-
The discussion on the matter is currently tangled in the question of just what is a detached item. (Is it one not reachable from the root, or is it one that has no parent and isn't the root? The definition affects how children of detached items are described.) Sep 12 at 21:02
When I need this in Tcl, I create a detached item with a special name, like "detached". Then whenever I want to detach an item, I just move it under the "detached" item. Getting a list of detached items is then just a matter of listing the children of the "detached" item.
ttk::treeview .tv
.tv detach [list [.tv insert {} end -id detached]]
# Create items
set item [.tv insert {} end -text "Some item"]
# Detach the item
.tv move $item detached end
# Get the detached items
puts [.tv children detached]
Of course this requires the discipline to detach items using the move command, and not use the detach command or the children command with only a partial list of children.