Add properties for getting the current pressed button

Either the index of the button (because the caller might be using the
radioset as a menu of sorts, which maps to some other vector-like
structure), or the button itself.
This commit is contained in:
Dave Pearson
2023-02-20 15:09:08 +00:00
parent fe1261c1ba
commit 3ee35f64d1

View File

@@ -76,3 +76,23 @@ class RadioSet(Container):
if button.value and button != event.input:
button.value = False
break
@property
def pressed_index(self) -> int:
"""The index of the currently-pressed button.
Note:
If no button is pressed the value will be `-1`.
"""
for index, button in enumerate(self._buttons):
if button.value:
return index
return -1
@property
def pressed_button(self) -> RadioButton | None:
"""The currently-pressed button, or `None` none are pressed."""
for button in self._buttons:
if button.value:
return button
return None