Skip to content

When a ListItem is Selected how do I return the Label text? #1840

Answered by davep
showipintbri asked this question in Q&A
Discussion options

You must be logged in to vote

A ListItem is a container widget, that can contain other widgets; in the example you point at it will contain a Label; it could, if you wanted, be any other kind of widget. The point here being that a ListItem doesn't have a label; it's just a container of other things.

If you want the ListItems of your ListView to contain widgets that also let you get a string label back, likely the best approach is to make such a widget of your own. So, taking the list_view.py example a little further:

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

class LabelItem(ListItem):

    def __init__(self, label: str) -> None:
        super().__init__()…

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by davep
Comment options

You must be logged in to vote
1 reply
@davep
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants