Is there builtin way to get a ToggleButton group's current selection?
Asked Answered
P

1

21

I was just browsing through the source in search of a way to do this but didn't see any. I want to be sure though, as I may have missed something. Is there a builtin way, like a method? When I have some ToggleButtons that are in a group together, I want to be able to get the value(text value I suppose), of the currently selected(state == 'down') button. I know I can roll my own way to do this without much hassle, but it seems strange that it wouldn't already exist.

After inspection of the docs and the source, I find this to be the easiest way so far:

from kivy.uix.togglebutton import ToggleButton as TB

current = [t for t in TB.get_widgets('group') if t.state=='down'][0]
value = current.text

While this isn't very long or hard to do, it would be nice to be able to do something like:

WARNING: Fictional Code

value = TB.get_widgets('group').selected
Profundity answered 1/8, 2015 at 14:57 Comment(3)
Up-voting because the Fictional Code warning made me LOL. The question itself was good too.Lydalyddite
Just remember that if allow_no_selection is set, there may be a group without a button in down state.Morrissette
@Morrissette Very true. I guess you could maybe wrap the code in a try/except block or somethingProfundity
B
4

No that is not possible with builtins. But here is how I would do it:

tb = next( (t for t in TB.get_widgets('group') if t.state=='down'), None)
test = tb.text if tb else None
Bernal answered 19/11, 2016 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.