Gtk.RadioButton
继承关系
Gtk.RadioButton复选框。Gtk.RadioButton是Gtk.CheckButton的直接子类
Methods
方法修饰词 | 方法名及参数 |
---|---|
static | new (group) |
static | new_from_widget (radio_group_member) |
static | new_with_label (group, label) |
static | new_with_label_from_widget (radio_group_member, label) |
static | new_with_mnemonic (group, label) |
static | new_with_mnemonic_from_widget (radio_group_member, label) |
get_group () | |
join_group (group_source) | |
set_group (group) |
Virtual Methods
do_group_changed () |
Properties
Name | Type | Flags | Short Description |
---|---|---|---|
group | Gtk.RadioButton | w | RadioButton 所属的组别. |
Signals
Name | Short Description |
---|---|
group-changed | RadioButton所属组别改变后,发送此信号. |
例子
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/14
# section 012
TITLE = "RadioButton"
DESCRIPTION = """
A single radio button performs the same basic function as a Gtk.CheckButton,
as its position in the object hierarchy reflects.
It is only when multiple radio buttons are grouped together
that they become a different user interface component in their own right
"""
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class RadioButtonWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="RadioButton Demo")
self.set_border_width(10)
grid = Gtk.Grid()
self.add(grid)
button1 = Gtk.RadioButton.new_with_label_from_widget(None, "Button 1")
button1.connect("toggled", self.on_button_toggled, "1")
grid.attach(button1, 0, 0, 1, 1)
button2 = Gtk.RadioButton.new_from_widget(button1)
button2.set_label("Button 2")
button2.connect("toggled", self.on_button_toggled, "2")
grid.attach(button2, 1, 0, 1, 1)
# press alt+u
button3 = Gtk.RadioButton.new_with_mnemonic_from_widget(button1,
"B_utton 3")
button3.connect("toggled", self.on_button_toggled, "3")
grid.attach(button3, 2, 0, 1, 1)
button4 = Gtk.RadioButton.new_with_label(None, "Button4")
button4.connect("toggled", self.on_button_toggled, "4")
grid.attach(button4, 0, 1, 1, 1)
button5 = Gtk.RadioButton.new_with_label(None, "Button5")
button5.connect("toggled", self.on_button_toggled, "5")
grid.attach(button5, 1, 1, 1, 1)
button5.join_group(button4)
# press alt+b
button6 = Gtk.RadioButton.new_with_mnemonic(None, "_Button6")
button6.connect("toggled", self.on_button_toggled, "6")
button6.join_group(button5)
grid.attach(button6, 2, 1, 1, 1)
# button2.set_active(True)
@staticmethod
def on_button_toggled(button, name):
if button.get_active():
state = "on"
else:
state = "off"
print("Button", name, "was turned", state)
def main():
win = RadioButtonWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
这个例子中有两组RadioButton,Button1,2,3为一组,剩下的为另一组。
button1 = Gtk.RadioButton.new_with_label_from_widget(None, "Button 1")
Button1使用new_with_label_from_widget()方法创建,并且第一个参数为None,那么它就是新的一个组。
button2 = Gtk.RadioButton.new_from_widget(button1)
button2使用new_from_widget()方法创建,并且传入button1为参数,意思是将button2添加到button1这个组中。
button3 = Gtk.RadioButton.new_with_mnemonic_from_widget(button1,
"B_utton 3")
button3的创建与button2创建的不同之处就是,button3使用了助记符,当按下alt+u时,选中button3.
第二组的主要是使用new_with_label(None,”“)方法创建,使用
join_group()方法,将一个button添加到另一个button所在的小组中
button5.join_group(button4)