Skip to content

Using a dict as a reactive #2538

Answered by davep
mattloose asked this question in Q&A
Discussion options

You must be logged in to vote

You may find #2193 helpful here.

A fairly useless but hopefully illustrative example could be:

from textual.app      import App, ComposeResult
from textual.reactive import reactive
from textual.widgets  import Header, Footer, Label

class DictReactiveApp( App[ None ] ):

    my_dict = reactive(dict(), always_update=True, init=False)

    BINDINGS = [
        ( "space", "change", "" ),
    ]

    def compose( self ) -> ComposeResult:
        yield Header()
        yield Label(str(self.my_dict))
        yield Footer()

    def watch_my_dict(self) -> None:
        self.query_one(Label).update(str(self.my_dict))

    def action_change( self ) -> None:
        self.my_dict[len(self.my_dict)] = l…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by mattloose
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2537 on May 10, 2023 10:31.