Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
✨ Example of extending a ListView item
Browse files Browse the repository at this point in the history
  • Loading branch information
davep committed Nov 12, 2023
1 parent bad49bc commit 557fad6
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions list_view_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""For a question on Discord."""

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

class URLItem(ListItem):

def __init__(self, title: str, url: str) -> None:
super().__init__()
self.title = title
self.url = url

def compose(self) -> ComposeResult:
yield Label(self.title)


class ListViewExampleApp(App[None]):

def compose(self) -> ComposeResult:
yield Label("Chosen will go here", id="result")
yield ListView(
URLItem("Textual", "https://textual.textualize.io/"),
URLItem("Rich", "https://rich.readthedocs.io/en/stable/introduction.html"),
URLItem("Textualize", "https://textualize.io/"),
)

@on(ListView.Selected)
def url_choice(self, event: ListView.Selected) -> None:
assert isinstance(event.item, URLItem)
self.query_one("#result", Label).update(event.item.url)

if __name__ == "__main__":
ListViewExampleApp().run()

### list_view_urls.py ends here

0 comments on commit 557fad6

Please sign in to comment.