From b40381f60367a13fec19c5a061bdddc7884f8041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20V=C3=A1zquez=20Blanco?= Date: Tue, 20 Dec 2022 14:59:28 +0100 Subject: [PATCH 1/2] Add an example of dynamic bindings --- examples/dynamic_bindings.py | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/dynamic_bindings.py diff --git a/examples/dynamic_bindings.py b/examples/dynamic_bindings.py new file mode 100644 index 0000000000..40ce4a73eb --- /dev/null +++ b/examples/dynamic_bindings.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +''' +Main file for the BT buddies app. +''' + +from textual.app import App, ComposeResult +from textual.widgets import Header, Footer +from textual.binding import Binding +from textual.reactive import reactive + + +from textual import log + + +class DynamicBindingsApp(App): + ''' + A TUI application that allows you to change binding text dynamically. + ''' + binding_description : reactive[str | None] = reactive("Toggle on") + + BINDINGS = [ + Binding(key="t", action="toggle", description=binding_description), + ] + + def compose(self) -> ComposeResult: + '''Build the UI''' + yield Header(show_clock=True) + yield Footer() + + def action_toggle(self) -> None: + '''Toggle callback''' + self.binding_description = "Toggle on" if "off" in self.binding_description else "Toggle off" + + +if __name__ == "__main__": + app = DynamicBindingsApp() + app.run() From c8ef04e86a08ee7496fd3cde7bf10a29317aa1dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20V=C3=A1zquez=20Blanco?= Date: Thu, 22 Dec 2022 21:31:48 +0100 Subject: [PATCH 2/2] Sort some imports and delete non related comments --- examples/dynamic_bindings.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/dynamic_bindings.py b/examples/dynamic_bindings.py index 40ce4a73eb..a42a0cb6c3 100644 --- a/examples/dynamic_bindings.py +++ b/examples/dynamic_bindings.py @@ -1,16 +1,13 @@ #!/usr/bin/env python ''' -Main file for the BT buddies app. +Example that shows dynamic bindings. ''' from textual.app import App, ComposeResult -from textual.widgets import Header, Footer from textual.binding import Binding from textual.reactive import reactive - - -from textual import log +from textual.widgets import Footer, Header class DynamicBindingsApp(App):