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

Select constructor shouldn't send Changed message #4391

Closed
willmcgugan opened this issue Apr 5, 2024 Discussed in #4368 · 1 comment
Closed

Select constructor shouldn't send Changed message #4391

willmcgugan opened this issue Apr 5, 2024 Discussed in #4368 · 1 comment

Comments

@willmcgugan
Copy link
Collaborator

Discussed in #4368

Originally posted by villekr March 31, 2024
After change in #3869 all widgets send 'Changed' events even during the initialization. What is the correct way to ignore these 'Changed' events during the initialization? @rodrigogiraoserrao

Let's say I have the following situation where 'secondary' Select-widget's selection depends on the selected value of 'primary' Select-widget. Initially both have some values.

Expected:

  • primary = "drink"
  • secondary = "juice"

Actual:

  • primary = "drink"
  • secondary = "water"
from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Header, Select

ITEMS = {"food": ["pizza", "burger", "salad"], "drink": ["water", "soda", "juice"]}


class SelectApp(App):
    CSS_PATH = "select.tcss"
    primary = "drink"
    secondary = "juice"

    def compose(self) -> ComposeResult:
        primary_items = [(item, item) for item in ITEMS]
        secondary_items = [(item, item) for item in ITEMS[self.primary]]
        yield Header()
        yield Select(
            primary_items,
            value=self.primary,
            prompt="Select Category",
            id="primary",
            classes="settings_option",
            allow_blank=False,
        )
        yield Select(
            secondary_items,
            value=self.secondary,
            prompt="Select Item",
            id="secondary",
            classes="settings_option",
            allow_blank=False,
        )
        self.title = f"{self.primary=} {self.secondary=}"

    @on(Select.Changed)
    def select_changed(self, event: Select.Changed) -> None:
        if event.control.id == "primary":
            self.primary = event.value
            items = ITEMS[event.value]
            widget: Select = self.query_one("#secondary")
            widget.set_options((item, item) for item in items)
        if event.control.id == "secondary":
            self.secondary = event.value
        self.title = f"{self.primary=} {self.secondary=}"


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

```</div>
Copy link

github-actions bot commented Apr 6, 2024

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

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