| from textual.app import App, ComposeResult | |
| from textual.color import Color | |
| from textual.widgets import Footer, Static | |
| class Bar(Static): | |
| pass | |
| class BindingApp(App): | |
| CSS_PATH = "binding01.css" | |
| BINDINGS = [ | |
| ("r", "add_bar('red')", "Add Red"), | |
| ("g", "add_bar('green')", "Add Green"), | |
| ("b", "add_bar('blue')", "Add Blue"), | |
| ] | |
| def compose(self) -> ComposeResult: | |
| yield Footer() | |
| def action_add_bar(self, color: str) -> None: | |
| bar = Bar(color) | |
| bar.styles.background = Color.parse(color).with_alpha(0.5) | |
| self.mount(bar) | |
| self.call_later(self.screen.scroll_end, animate=False) | |
| if __name__ == "__main__": | |
| app = BindingApp() | |
| app.run() | |