Triggering ListView.Selected #1567
-
For whatever reason, neither ListView.Highlight or Listview.Selected are calling handlers. I am working on Macos 13.0.1, with Python 3.11 class fileReader(App):
files = reactive([])
def compose(self) -> ComposeResult:
for file in os.listdir():
if file[0] != ".":
self.files.append(file)
logging.info(f"Found file: {file}")
yield Header()
yield Vertical(
ListView(
*[ListItem( Label(f"{file}"), id=f"{file}" ) for file in self.files]
),
TextLog(name="log", wrap=True, classes="box")
)
def on_listview_highlighted(self, event: ListView.Highlighted) -> None:
itemID = event.ListView.item.id
logging.info(f"Selected: {itemID}") Unless I am doing something incredibly wrong, I don't understand why it shouldn't be working. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The event handler for the ListView highlight message should be Here's a little standalone variation on what you've got above that will show the events in a import os
from textual.app import App, ComposeResult
from textual.containers import Horizontal
from textual.widgets import Header, Footer, ListView, ListItem, TextLog, Label
from textual.reactive import reactive
class FileReader( App[ None ] ):
CSS = """
ListView {
width: 1fr;
}
TextLog {
width: 1fr;
}
"""
files = reactive([])
def compose( self ) -> ComposeResult:
for file in os.listdir():
if file[0] != ".":
self.files.append(file)
yield Header()
yield Horizontal(
ListView( *[
ListItem( Label( f"{file}") , id=f"{file}" )
for file in self.files
] ),
TextLog()
)
yield Footer()
def on_list_view_highlighted( self, event: ListView.Highlighted ) -> None:
self.query_one( TextLog ).write( event )
def on_list_view_selected( self, event: ListView.Selected ) -> None:
self.query_one( TextLog ).write(event )
if __name__ == "__main__":
FileReader().run() |
Beta Was this translation helpful? Give feedback.
The event handler for the ListView highlight message should be
on_list_view_highlighted
-- note the_
inlist_view
. One of the rules for the handler name that is looked for is CamelCase gets turned into snake_case.Here's a little standalone variation on what you've got above that will show the events in a
TextLog
: