How to select a tree node and expand it #2135
-
I have a populated a How can I select that node and expand the tree to show it exactly as if the user had manually expanded clicked on it? I see a few methods like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You might find the code references in the docs for Tree and TreeNode helpful.
EDIT: It looks like you could also use from textual.app import App, ComposeResult
from textual.widgets import Tree
class TreeApp(App):
def compose(self) -> ComposeResult:
tree: Tree[dict] = Tree("Dune")
tree.root.expand()
characters = tree.root.add("Characters")
characters.add_leaf("Paul")
characters.add_leaf("Jessica")
characters.add_leaf("Chani")
yield tree
node_to_expand = tree.get_node_by_id(characters.id)
tree.select_node(node_to_expand)
node_to_expand.expand()
if __name__ == "__main__":
app = TreeApp()
app.run() |
Beta Was this translation helpful? Give feedback.
You might find the code references in the docs for Tree and TreeNode helpful.
select_node
will only "Move the cursor to the given node". You can change the expanded state of aTreeNode
using thetoggle
orexpand
methods.EDIT: It looks like you could also use
action_toggle_node
, but only after ensuring that the selected node has refreshed:tree.call_after_refresh(tree.action_toggle_node)