-
I have written a lot of Python, but am not really fluent in classes and object oriented programming--I was a C and Verilog person for many years . . . So I am trying to bind the 'j' and 'k' keys to scroll the MarkdownViewer in the example program markdown.py. I think what I want to do is bind 'j', for example, to generate a MouseScrollDown event, but I have no idea how to generate an event. Either that, or figure out how to rewrite the scroll_down and scroll_up functions that are in widget.py so that they reference my instance of MarkdownViewer. This seems complicate (particularly to me!) since the code already exists, I just need to figure out how to reference it. The only success I've had is to hack the widget.py source and add my bindings there. That works, but obviously isn't a solution. If someone can point me to an example that does a binding like this that would be perfect--I can learn from it. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Unless otherwise told not to, bindings inherit. So you can generally inherit from a widget and add your own bindings. For example: from pathlib import Path
from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, MarkdownViewer
from textual.binding import Binding
class ViLikeMarkdownViewer( MarkdownViewer ):
BINDINGS = [
Binding( "j", "up", "", show=False ),
Binding( "k", "down", "", show=False ),
]
def action_up( self ) -> None:
self.scroll_up()
def action_down( self ) -> None:
self.scroll_down()
class ViLikeMarkdownApp( App[ None ] ):
def compose( self ) -> ComposeResult:
yield Header()
yield ViLikeMarkdownViewer( Path( "../textual/README.md" ).read_text() )
yield Footer()
if __name__ == "__main__":
ViLikeMarkdownApp().run() |
Beta Was this translation helpful? Give feedback.
Unless otherwise told not to, bindings inherit. So you can generally inherit from a widget and add your own bindings. For example: