Ensure the pressed button index is correct in the message

At the time that we're raising RadioSet.Changed, we can't be sure that the
class updates and the like have finished post RadioButton.watch_value
happening; this means that the "public" route to getting the pressed index
could be wrong (or rather, not quite up to date yet).

Meanwhile, here, we know the button involved, so we go looking for its index
directly.
This commit is contained in:
Dave Pearson
2023-02-23 21:47:32 +00:00
parent d341b8b1b8
commit 8d726fba88

View File

@@ -87,7 +87,12 @@ class RadioSet(Container):
"""A reference to the `RadioSet` that was changed."""
self.pressed = pressed
"""The `RadioButton` that was pressed to make the change."""
self.index = sender.pressed_index
# Note: it would be cleaner to use `sender.pressed_index` here,
# but we can't be 100% sure all of the updates have happened at
# this point, and so we can't go looking for the index of the
# pressed button via the normal route. So here we go under the
# hood.
self.index = sender._nodes.index(pressed)
"""The index of the `RadioButton` that was pressed to make the change."""
def on_radio_button_changed(self, event: RadioButton.Changed) -> None: