What's the right way to programmatically set the selected RadioButton within a RadioSet? #3983
-
|
I have a RadioSet with a bunch of RadioButtons, e.g.: RadioSet(
RadioButton("task 1a", classes="task1", value=True),
RadioButton("task 1b", classes="task1", value=False),
RadioButton("task 2a", classes="task2", value=False),
RadioButton("task 2b", classes="task2", value=False),
id = "target_radio_set"
)Only a subset of them should be visible at any time, depending on the state of the rest of the program. I've figured out two ways to do that:
buttons = self.query("#target_radio_set>RadioButton.task1")
for obj in buttons:
obj.display = True
.task2_active #target_radio_set>RadioButton{
display: none;
&.task2 {
display: block;
}
}However, once I have the proper radio buttons displaying, I want to either totally clear the RadioSet's state so that no buttons are active, or to default to the first displayed button being set to active. Is it possible to do either of these programmatically? (The important bit here is that if I query the RadioSet's state via I've tried setting I also tried |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
I feel you're trying to do a little more with As an illustration: from textual import on
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import RadioSet, Select
class DynamicRadioSetApp(App[None]):
def compose(self) -> ComposeResult:
yield Select[int]((("The Beatles", 0), ("Radiohead", 1),("REM", 2)))
with Vertical(id="band-picker"):
yield RadioSet()
@on(Select.Changed)
async def pick_the_best_member(self, event: Select.Changed) -> None:
set_container = self.query_one("#band-picker")
await set_container.query_one(RadioSet).remove()
members = []
if event.value == 0:
members = ["John", "Paul", "George", "Ringo"]
elif event.value == 1:
members = ["Thom", "Jonny", "Ed", "Philip"]
elif event.value == 2:
members = ["Bill", "Peter", "Mike", "Michael"]
await set_container.mount(RadioSet(*members))
if __name__ == "__main__":
DynamicRadioSetApp().run() |
Beta Was this translation helpful? Give feedback.
I feel you're trying to do a little more with
RadioSetthan it was intended to do. If I wanted aRadioSetwhose content depended on the state of some other part of my application, I'd probably take an approach where Iremovethe existing one andmountfresh one in its place with the desired buttons.As an illustration: