Start fleshing out a RadioSet widget

Much more to do with this, but I just wanted to get a feel for the core
working.
This commit is contained in:
Dave Pearson
2023-02-20 11:51:29 +00:00
parent bf727fcb9f
commit 594c1faad6
3 changed files with 42 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ if typing.TYPE_CHECKING:
from ._placeholder import Placeholder
from ._pretty import Pretty
from ._radio_button import RadioButton
from ._radio_set import RadioSet
from ._static import Static
from ._switch import Switch
from ._text_log import TextLog
@@ -47,6 +48,7 @@ __all__ = [
"Placeholder",
"Pretty",
"RadioButton",
"RadioSet",
"Static",
"Switch",
"TextLog",

View File

@@ -14,6 +14,7 @@ from ._markdown import MarkdownViewer as MarkdownViewer
from ._placeholder import Placeholder as Placeholder
from ._pretty import Pretty as Pretty
from ._radio_button import RadioButton as RadioButton
from ._radio_set import RadioSet as RadioSet
from ._static import Static as Static
from ._switch import Switch as Switch
from ._text_log import TextLog as TextLog

View File

@@ -0,0 +1,39 @@
"""Provides a RadioSet widget, which groups radio buttons."""
from __future__ import annotations
from ..containers import Container
from ._radio_button import RadioButton
class RadioSet(Container):
"""Widget for grouping a collection of radio buttons into a set."""
def __init__(self, *buttons: str | RadioButton) -> None:
"""Initialise the radio set.
Args:
buttons: A collection of labels or `RadioButton`s to group together.
Note:
When a `str` label is provided, a `RadioButton` will be created from it.
"""
self._buttons = [
(button if isinstance(button, RadioButton) else RadioButton(button))
for button in buttons
]
super().__init__(*self._buttons)
def on_radio_button_selected(self, event: RadioButton.Selected) -> None:
"""Respond to a radio button in the set being selected be the user.
Args:
event: The event being raised.
"""
# For each of the buttons we're responsible for...
for button in self._buttons:
# ...if it's on and it's not the selected button...
if button.value and button != event.input:
# ...turn it off.
button.value = False
break