开发者

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

开发者 https://www.devze.com 2022-12-12 16:56 出处:网络
Is there a way to uncheck all radio button开发者_如何学Pythons 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

Is there a way to uncheck all radio button开发者_如何学Pythons 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.


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


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.


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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消