Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add an example of dynamic bindings #1412

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/dynamic_bindings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python

'''
Example that shows dynamic bindings.
'''

from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.reactive import reactive
from textual.widgets import Footer, Header


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()