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

Documented Actions/Namespace MRE not quite right? #5422

Open
anschweitzer opened this issue Dec 21, 2024 · 1 comment · May be fixed by #5430
Open

Documented Actions/Namespace MRE not quite right? #5422

anschweitzer opened this issue Dec 21, 2024 · 1 comment · May be fixed by #5430

Comments

@anschweitzer
Copy link

It seems that the MRE for "The following example defines a custom widget with its own set_background action" does not show per-widget actions, but only App-level actions. As is, clicking the link on the individual widget sets the background for the whole App, instead of setting the background for the widget clicked, which I think is the intention.

Changing

@click=app.set_background

to

@click=set_background

appears to fix the issue.

See also possible duplicate #5394.

Complete updated MRE

from textual.app import App, ComposeResult
from textual.widgets import Static

TEXT = """
[b]Set your background[/b]
[@click=set_background('cyan')]Cyan[/]
[@click=set_background('magenta')]Magenta[/]
[@click=set_background('yellow')]Yellow[/]
"""


class ColorSwitcher(Static):
    def action_set_background(self, color: str) -> None:
        self.styles.background = color


class ActionsApp(App):
    CSS_PATH = "actions05.tcss"
    BINDINGS = [
        ("r", "set_background('red')", "Red"),
        ("g", "set_background('green')", "Green"),
        ("b", "set_background('blue')", "Blue"),
    ]

    def compose(self) -> ComposeResult:
        yield ColorSwitcher(TEXT)
        yield ColorSwitcher(TEXT)

    def action_set_background(self, color: str) -> None:
        self.screen.styles.background = color


if __name__ == "__main__":
    app = ActionsApp()
    app.run()

Textual Diagnostics

Versions

Name Value
Textual 1.0.0
Rich 13.5.2

Python

Name Value
Version 3.11.9
Implementation CPython
Compiler Clang 15.0.0 (clang-1500.3.9.4)
Executable .../venv/bin/python

Operating System

Name Value
System Darwin
Release 24.2.0
Version Darwin Kernel Version 24.2.0: Fri Dec 6 19:01:59 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6000

Terminal

Name Value
Terminal Application iTerm.app (3.5.10)
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=214, height=39
legacy_windows False
min_width 1
max_width 214
is_terminal True
encoding utf-8
max_height 39
justify None
overflow None
no_wrap False
highlight None
markup None
height None
Copy link

We found the following entry 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

TomJGooding added a commit to TomJGooding/textual that referenced this issue Dec 24, 2024
Fix the code example for action namespaces, where clicking the links
should update the background for that widget only.

Fixes Textualize#5422

Co-authored-by: Andrew Schweitzer <[email protected]>
@TomJGooding TomJGooding linked a pull request Dec 24, 2024 that will close this issue
3 tasks
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

Successfully merging a pull request may close this issue.

1 participant