Is there a way to uncheck all radio buttons in a group? (PyGTK)
Asked Answered
C

4

10

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.

Citified answered 21/11, 2009 at 1:13 Comment(1)
Not checked on startup? I have never seen that.Balf
L
3

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.

Lynda answered 21/11, 2009 at 1:30 Comment(12)
Thanks. I added a "None" radio button. :)Citified
Totally 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.Rugging
@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.Lynda
If 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.Acea
@SebastianWerk - Why? I don't see where a 'none of the above' selection wouldn't work just fine.Lynda
For 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“)Acea
So is there a solution to this?Incondensable
@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.Lynda
FYI: I have written a Gtk#-based follow-up question 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.Topic
@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 notIncondensable
A 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.Downswing
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.Villus
B
8

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()
Balf answered 21/11, 2009 at 2:8 Comment(2)
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.Acea
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).Cygnus
L
3

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.

Lynda answered 21/11, 2009 at 1:30 Comment(12)
Thanks. I added a "None" radio button. :)Citified
Totally 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.Rugging
@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.Lynda
If 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.Acea
@SebastianWerk - Why? I don't see where a 'none of the above' selection wouldn't work just fine.Lynda
For 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“)Acea
So is there a solution to this?Incondensable
@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.Lynda
FYI: I have written a Gtk#-based follow-up question 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.Topic
@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 notIncondensable
A 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.Downswing
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.Villus
P
1

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.

Pavo answered 21/11, 2009 at 1:25 Comment(3)
I tried to do that, but it will automatically re-enable one of the radio buttons in the group.Citified
Updated the answer after looking into the implementation.Pavo
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.Cygnus
E
1

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)

Eudo answered 7/9, 2015 at 1:53 Comment(1)
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.Cygnus

© 2022 - 2024 — McMap. All rights reserved.