-
After reading the docs, I have understood that you can only display text in trees. Is there any way to display something else in a tree, like a checkbox?. It is possible with Rich. |
Beta Was this translation helpful? Give feedback.
Answered by
davep
May 19, 2023
Replies: 1 comment 2 replies
-
Yes, labels of nodes in from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, Tree
class ToggleTree( Tree[ bool ] ):
def on_tree_node_selected( self, event: Tree.NodeSelected ) -> None:
if not event.node.children:
event.node.data = not event.node.data
if event.node.data:
event.node.label = ":thumbsup:"
else:
event.node.label = ":thumbsdown:"
event.node.tree.refresh()
class CheckTreeApp( App[ None ] ):
def compose( self ) -> ComposeResult:
yield Header()
yield ToggleTree( "Root" )
yield Footer()
def on_mount( self ) -> None:
tree = self.query_one( ToggleTree )
tree.root.add_leaf( ":thumbsdown:" )
tree.root.add_leaf( ":thumbsdown:" )
tree.root.add_leaf( ":thumbsdown:" )
if __name__ == "__main__":
CheckTreeApp().run() |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
sergi0g
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, labels of nodes in
Tree
can be Rich renderables (as long as they're no more than one line in length). Here's a silly example of setting up some leaf nodes that can be toggled when the user selects them: