tags:

views:

310

answers:

3

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.

A: 

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.

m5h
I tried to do that, but it will automatically re-enable one of the radio buttons in the group.
linkmaster03
Updated the answer after looking into the implementation.
m5h
+3  A: 

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.

Michael Kohne
Thanks. I added a "None" radio button. :)
linkmaster03
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.
Ali A
@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
+1  A: 

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()
DoR