Replies: 1 comment
-
When you recompose, Textual will destroy all widgets and replace them with newly composed widgets. This means that the button you set the focus on, no longer exists after recompose. For something like this, it is generally simpler to explicitly update the button. Add a watch method and then update your content: from __future__ import annotations
from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Button, Static
from textual.containers import Horizontal, Container
from textual.reactive import reactive
class CustomContainer(Container):
counter: int = reactive(0, init=False)
def __init__(self, counter: int):
super().__init__()
self.counter = counter
def compose(self) -> ComposeResult:
yield Static(f"{self.counter}", id="counter")
with Horizontal():
yield Button("+1", variant="success", id="add-value")
yield Button("-1", variant="primary", id="subtract-value")
def watch_counter(self, counter: int) -> None:
self.query_one("#counter", Static).update(str(counter))
@on(Button.Pressed, "#add-value")
def increase_value(self):
self.counter += 1
@on(Button.Pressed, "#subtract-value")
def decrease_value(self):
self.counter -= 1
class MyApp(App):
DEFAULT_CSS = """
CustomContainer {
height: 5;
}
Horizontal {
margin-top: 1;
}
"""
def compose(self) -> ComposeResult:
yield CustomContainer(0)
yield Button("Just button", variant="primary")
MyApp().run() If you want to use recompose, separate what you want to change from what you don't. Here's an example: from __future__ import annotations
from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Button, Static
from textual.containers import Horizontal, Container, VerticalGroup
from textual.reactive import reactive
class CounterDisplay(VerticalGroup):
counter = reactive(0, recompose=True)
def compose(self) -> ComposeResult:
yield Static(str(self.counter))
class CustomContainer(Container):
counter = reactive(0)
def __init__(self, counter: int):
super().__init__()
self.counter = counter
def compose(self) -> ComposeResult:
yield CounterDisplay().data_bind(counter=CustomContainer.counter)
with Horizontal():
yield Button("+1", variant="success", id="add-value")
yield Button("-1", variant="primary", id="subtract-value")
@on(Button.Pressed, "#add-value")
def increase_value(self):
self.counter += 1
@on(Button.Pressed, "#subtract-value")
def decrease_value(self):
self.counter -= 1
class MyApp(App):
DEFAULT_CSS = """
CustomContainer {
height: 5;
}
Horizontal {
margin-top: 1;
}
"""
def compose(self) -> ComposeResult:
yield CustomContainer(0)
yield Button("Just button", variant="primary")
MyApp().run() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Found a situation where set focus doesn't work. Tested on textual 0.86.3 and 0.83.0.
MRE:
I'd like to set focus on decrease/increase button after clicking it (and after recompose) by
set_focus
, but having this line brings no effect. Tried it withfocus_next
- no effect as well .Beta Was this translation helpful? Give feedback.
All reactions