Binding to focus widget? #1721
-
How might you add a binding to focus a particular widget? I've checked the documentation and see there's For example. let's say I wanted to add a vim-like binding to from textual.app import App, ComposeResult
from textual.widgets import Footer, Input, Static
class ExampleForm(Static):
def compose(self) -> ComposeResult:
yield Input(placeholder="Enter text here...")
class ExampleApp(App):
BINDINGS = [("q", "quit", "Quit"), ("i", "NotImplemented", "Focus Form")]
def compose(self) -> ComposeResult:
yield ExampleForm()
yield Footer()
def on_input_submitted(self) -> None:
self.query_one(Input).value = "" Many thanks in advance for any advice on this! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You're almost there with the content of your ...
BINDINGS = [ ..., ("i", "focus_input", "Focus Form" ) ]
...
def action_foocus_input( self ) -> None:
self.query_one(Input).focus() Of course, if you end up having more than one |
Beta Was this translation helpful? Give feedback.
You're almost there with the content of your
on_input_submitted
. As you note, widgets have afocus
method, so if want a binding to focus yourInput
widget you could have something like:Of course, if you end up having more than one
Input
on your screen you'll want to narrow it down, perhaps using anid
for theInput
you want to focus.