Skip to content

Documented Actions/Namespace MRE not quite right? #5422

@anschweitzer

Description

@anschweitzer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions