Textualize/textual

Improve the `ListView` documentation by explaining the `ListItem`

Closed this issue · 3 comments

I think the ListView documentation could be improved by explaining the ListItem.

Here's a simple example adapted from the docs. I don't think it is clear from the documentation how you might get the Label text when a ListItem is highlighted:

from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Label, ListItem, ListView


class ListViewExample(App):
    CSS = """
    Screen {
        align: center middle;
    }

    ListView {
        width: 30;
        height: auto;
    }
    """

    def compose(self) -> ComposeResult:
        yield ListView(
            ListItem(Label("One")),
            ListItem(Label("Two")),
            ListItem(Label("Three")),
        )

    @on(ListView.Highlighted)
    def notify_highlighted_label(self, event: ListView.Highlighted) -> None:
        highlighted = event.item
        self.notify(str(highlighted))


if __name__ == "__main__":
    app = ListViewExample()
    app.run()

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

Actually on reflection I don't think this is really necessary. ListItem is just a widget and querying the children is already covered elsewhere in the docs.

Don't forget to star the repository!

Follow @textualizeio for Textual updates.