Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to detect the selected node from a tree. #5523

Closed
DGRFL opened this issue Feb 13, 2025 · 4 comments
Closed

How to detect the selected node from a tree. #5523

DGRFL opened this issue Feb 13, 2025 · 4 comments

Comments

@DGRFL
Copy link

DGRFL commented Feb 13, 2025

In the attached App, pressing the button will indicate the highlighted and the selected node from the tree. In order to accomplish this, I needed to fetch the highlighted and selected event, and store the specific nodes in self.SEL_node and self.HIGH_node.
Isn't there an easier way to use an attribute from the tree variable itself? It removes the need for storing into instance variables.

from textual.app import App
from textual.widgets import Tree, Button, Log
from textual.message import Message


class MyApp(App):
    def compose(self):
        self.tree_panel = Tree("Root",id="boom")
        # Create the log panel
        self.log_panel = Log()

        # Create the start and stop buttons
        self.start_button = Button("Start", id="start_button")
        yield self.tree_panel
        yield self.log_panel
        yield self.start_button

    def on_tree_node_highlighted( self, event: Tree.NodeHighlighted ) -> None:
        """Called when the user click a file in the directory tree."""
        event.stop()
        nd = event.node
        self.HIGH_node = nd
        self.log_panel.write(f"Node highlighted  {nd.label}\n")

    def on_tree_node_selected( self, event: Tree.NodeSelected ) -> None:
        """Called when the user click a file in the directory tree."""
        event.stop()
        nd = event.node
        self.SEL_node = nd
        self.log_panel.write(f"Node selected  {nd.label}\n")

    def on_mount(self):
        # Populate the tree
        tree= self.tree_panel
        node = tree.root
        node = node.add("AAA")
        node = node.add("BBB"   )

    async def on_button_pressed(self, message: Message):
        button_id = message.control.id
        high_node = self.HIGH_node    
        if high_node:
                self.log_panel.write(f"Start action highlighted for {high_node.label}\n")
        selected_node = self.SEL_node    
        if selected_node:
                self.log_panel.write(f"Start action selected for {selected_node.label}\n")

app = MyApp()
app.run()


Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@TomJGooding
Copy link
Contributor

There's the Tree.cursor_node property, but that docstring might be a bit misleading as this returns the node currently under the cursor. I don't think the Tree stores any state of the last selected node.

@DGRFL
Copy link
Author

DGRFL commented Feb 14, 2025

THanks, and perhaps it is indeed not required to store it : adding the event is a good alternative

@DGRFL DGRFL closed this as completed Feb 14, 2025
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants