bastd.ui.radiogroup

UI functionality for creating radio groups of buttons.

 1# Released under the MIT License. See LICENSE for details.
 2#
 3"""UI functionality for creating radio groups of buttons."""
 4
 5from __future__ import annotations
 6
 7from typing import TYPE_CHECKING
 8
 9import ba
10
11if TYPE_CHECKING:
12    from typing import Any, Callable, Sequence
13
14
15def make_radio_group(check_boxes: Sequence[ba.Widget],
16                     value_names: Sequence[str], value: str,
17                     value_change_call: Callable[[str], Any]) -> None:
18    """Link the provided check_boxes together into a radio group."""
19
20    def _radio_press(check_string: str, other_check_boxes: list[ba.Widget],
21                     val: int) -> None:
22        if val == 1:
23            value_change_call(check_string)
24            for cbx in other_check_boxes:
25                ba.checkboxwidget(edit=cbx, value=False)
26
27    for i, check_box in enumerate(check_boxes):
28        ba.checkboxwidget(edit=check_box,
29                          value=(value == value_names[i]),
30                          is_radio_button=True,
31                          on_value_change_call=ba.Call(
32                              _radio_press, value_names[i],
33                              [c for c in check_boxes if c != check_box]))
def make_radio_group( check_boxes: Sequence[_ba.Widget], value_names: Sequence[str], value: str, value_change_call: Callable[[str], Any]) -> None:
16def make_radio_group(check_boxes: Sequence[ba.Widget],
17                     value_names: Sequence[str], value: str,
18                     value_change_call: Callable[[str], Any]) -> None:
19    """Link the provided check_boxes together into a radio group."""
20
21    def _radio_press(check_string: str, other_check_boxes: list[ba.Widget],
22                     val: int) -> None:
23        if val == 1:
24            value_change_call(check_string)
25            for cbx in other_check_boxes:
26                ba.checkboxwidget(edit=cbx, value=False)
27
28    for i, check_box in enumerate(check_boxes):
29        ba.checkboxwidget(edit=check_box,
30                          value=(value == value_names[i]),
31                          is_radio_button=True,
32                          on_value_change_call=ba.Call(
33                              _radio_press, value_names[i],
34                              [c for c in check_boxes if c != check_box]))

Link the provided check_boxes together into a radio group.