Is there a way to uncheck all radio buttons in a group with PyGTK? No radio buttons are checked on startup, so I think there must be a way to return them all to that unchecked state.
-
Not checked on startup? I have never seen that. – Isaiah Nov 21 '09 at 01:48
4 Answers
I agree with Michael, but for the record this can be done.
One way to do this would be to have a hidden radio button that you could activate, which would then cause all the visible ones to be inactive. Quick n' Dirty example:
import gtk
window = gtk.Window()
window.set_default_size(200, 200)
rb1 = gtk.RadioButton()
rb2 = gtk.RadioButton()
rb3 = gtk.RadioButton()
rb2.set_group(rb1)
rb3.set_group(rb2)
rb3.set_active(True)
hbox = gtk.HBox()
hbox.add(rb1)
hbox.add(rb2)
hbox.add(rb3)
button = gtk.Button("Click me")
button.connect("clicked", lambda x: rb3.set_active(True))
hbox.add(button)
window.add(hbox)
window.show_all()
rb3.hide()
gtk.main()

- 4,201
- 4
- 27
- 40
-
At least at my Computer it also works, if the None-Element is the first one in a group and simply not added to any box at all. – Sebastian Werk Jan 09 '13 at 05:19
-
Worked for me, despite all the naysayers. In my GUI, I need this for cases where the RadioButtons are desensitised and the old values irrelevant (it's a non-dismissable confirmation step prior to loading in new data). – underscore_d Jan 24 '16 at 20:21
There shouldn't be. By their nature, a radio button group is a 'pick one of many' type of control. From a human perspective, the idea is that one of them is always selected. The framework really should enforce that, so there really shouldn't be a 'none selected' state for the group. The none-selected state of a radio button group (in frameworks where it's possible) is very confusing to users, because that's not a state that they can get the control into.
If you want a 'none selected' state, I'd say you should add a 'none' element to the group, OR chose a different control type that conforms to what you want to do with it.

- 11,888
- 3
- 47
- 79
-
3
-
6Totally disagree. Imagine a situation where the radio group is insensitized by interaction with another widget. None of the items should appear selected. GTK itself won't force you to behave by some imaginary rules. – Ali Afshar Nov 25 '09 at 18:21
-
2@Ali A:You still shouldn't be able to force a no-selection. The group should become visually different (greyed-out on most systems) as an indication that it can't be interacted with. In a good UI, the previous selection should still be present, because if you revert the setting of the other control, the radio buttons should go back to what they were before you touched the other control. – Michael Kohne Nov 25 '09 at 21:35
-
7If you have a questionaire with radio-buttons, and you don’t want any bias in it, you need a way to have no button selected. – Sebastian Werk Jan 08 '13 at 16:12
-
@SebastianWerk - Why? I don't see where a 'none of the above' selection wouldn't work just fine. – Michael Kohne Jan 08 '13 at 18:59
-
4For example, if you want to check, if people understood a task, they had to perform, this could be a regular answer. If this is preselected, you might influence the distribution of answers. Furthermore such an option is not always wanted; in some psychological experiments you want people to select one regular answer, even if they tend to not select any of them (Example: Would you rather prefer “Obama and a Republican Majority in Senate” or “Romney and a Democratic Majority in Senate“) – Sebastian Werk Jan 09 '13 at 05:13
-
-
1@RakshithRavi - Well, if you've read my answer and decided to break UI convention and confuse your users, then keep reading other answers, which show how to actually accomplish this. – Michael Kohne Sep 03 '15 at 01:14
-
2FYI: I have written a [Gtk#-based follow-up question](http://stackoverflow.com/questions/32386542/how-to-prevent-auto-selection-of-radio-buttons) referencing this answer. As Gtk# quite directly exposes the interface of Gtk, *maybe* you have an idea there, in case you have any time to look at it. – O. R. Mapper Sep 03 '15 at 22:42
-
2@MichaelKohne Sebastian's comment is an excellent example of where this could be useful. The thing is, I am making a framework which uses Gtk. So for that, I need full control over the widgets. So I need to be able to control whether the radio button is selected or not – Rakshith Ravi Sep 04 '15 at 18:36
-
1A valid use case for having no buttons initially selected would be a game such as Yahtzee where you can only select one scoring slot each turn, but none should be selected at the beginning of your turn. – Dave F Jul 02 '16 at 19:10
-
I equate all radiobuttons being unchecked the same as a 'null' value for a tri-state checkbox. Without the extra 'None' option, it can be dangerous to default to any one button without the user checking it. – ProfK Mar 11 '20 at 12:17
Looked it up in the source code and set_active simply simulates a click on the button if the new state is different from the old one. The radio button code then checks to see if there is another radio button in the group active and if not, it refuses to change as you noticed.
From what it looks the first radio button should always be set to active when you create the group (as expected). If it doesn't show it is likely a bug, it would be interested to see if radio_button.get_active
is True
for the first button you create (even if it doesn't show up in the UI).
I agree with Michael Kohne though that you should look into another UI element if you want to make all the radio buttons unselected.

- 123,625
- 4
- 33
- 21
-
I tried to do that, but it will automatically re-enable one of the radio buttons in the group. – linkmaster03 Nov 21 '09 at 01:26
-
-
Thanks for the confirmation from the code! Kinda weird that they would give us a function to `set_active(false)` (in `gtkmm` parlance) yet not actually honour it in all cases, but oh well. – underscore_d Jan 24 '16 at 20:22
My solution, which is notably not encouraged, Is to have a radio button in the same group that isn't shown on screen.
In Glade you can 'add widget as toplevel' in the context menu of the widget add button. In code I would imaging it's basically just don't add the widget to any displayed gui, or carefully don't .show() it (including .showall() on a parent)

- 16,657
- 15
- 135
- 147
-
Yup, I just added a dummy RadioButton to my containing class and simply don't display it anywhere. On desensitisation, it gets selected, so that all the actual (visible) buttons are deselected. – underscore_d Jan 24 '16 at 20:23