Is it possible to override default widget bindings? #4576
-
I'm trying to add the ability to delete entire words at a time within an from textual.binding import Binding
from textual.widgets import Input
from textual.app import App, ComposeResult
class CustomInput(Input):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.BINDINGS += [
Binding("ctrl+backspace", "delete_left_word", "delete left to start of word", show=False),
Binding("ctrl+delete", "delete_right_word", "delete right to end of word", show=False),
]
class MainApp(App):
def compose(self) -> ComposeResult:
yield CustomInput(placeholder="How can I help?")
if __name__ == "__main__":
app = MainApp()
app.run() Not sure if you can see what/if I'm doing something wrong? Many thanks for any help, and this amazing lib! |
Beta Was this translation helpful? Give feedback.
Answered by
TomJGooding
May 30, 2024
Replies: 1 comment
-
Bindings are inherited by default, so you don't need to mess with from textual.widgets import DataTable
from textual.app import App, ComposeResult
class ViLikeDataTable(DataTable):
BINDINGS = [
("j", "cursor_down", "Down"),
("k", "cursor_up", "Up"),
("h", "cursor_left", "Left"),
("l", "cursor_right", "Right"),
]
class ExampleApp(App):
def compose(self) -> ComposeResult:
yield ViLikeDataTable()
def on_mount(self) -> None:
table = self.query_one(DataTable)
table.add_columns("C1", "C2", "C3")
table.add_row(*("R1C1", "R1C2", "R1C3"))
table.add_row(*("R2C1", "R2C2", "R2C3"))
table.add_row(*("R3C1", "R3C2", "R3C3"))
if __name__ == "__main__":
app = ExampleApp()
app.run() You might find some key combinations don't make it to the app - see the FAQ: https://textual.textualize.io/FAQ/#why-do-some-key-combinations-never-make-it-to-my-app |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
asmith26
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bindings are inherited by default, so you don't need to mess with
__init__
. Here's a quick example - notice that the arrow keys still work to navigate the table: