Ignore changed-events during initialization #4368
-
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:
Actual:
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() |
Beta Was this translation helpful? Give feedback.
Answered by
willmcgugan
Apr 5, 2024
Replies: 1 comment 1 reply
-
Any comments from anyone? @rodrigogiraoserrao @TomJGooding Workaround would be to check if the value has actually changed but this just feels unnesessary.
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this is a bug. The initial value set in the constructor shouldn't send a changed message.