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

Set focus doesn't work properly #5269

Open
matkudela opened this issue Nov 21, 2024 · 1 comment
Open

Set focus doesn't work properly #5269

matkudela opened this issue Nov 21, 2024 · 1 comment

Comments

@matkudela
Copy link

Found a situation where set focus doesn't work. Tested on textual 0.86.3 and 0.83.0.
MRE:

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, recompose=True)

    def __init__(self, counter: int):
        super().__init__()
        self.counter = counter

    def compose(self) -> ComposeResult:
        yield Static(f"{self.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
        increase_button = self.query_exactly_one("#add-value")
        self.screen.set_focus(increase_button)

    @on(Button.Pressed, "#subtract-value")
    def decrease_value(self):
        self.counter -= 1
        decrease_button = self.query_exactly_one("#subtract-value")
        self.screen.set_focus(decrease_button)


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

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 with focus_next - no effect as well .

Copy link

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant