单选按钮¶
单选按钮是带有“选中”指示器的按钮,它们属于一组相似的按钮。组内只能选择一个单选按钮。
GTK 使用 复选框 组来实现“单选”行为。
GtkWidget *live = gtk_check_button_new_with_label ("Live");
GtkWidget *laugh = gtk_check_button_new_with_label ("Laugh");
GtkWidget *love = gtk_check_button_new_with_label ("Love");
gtk_check_button_set_group (GTK_CHECK_BUTTON (laugh),
GTK_CHECK_BUTTON (live));
gtk_check_button_set_group (GTK_CHECK_BUTTON (love),
GTK_CHECK_BUTTON (live));
live = Gtk.CheckButton(label="Live")
laugh = Gtk.CheckButton(label="Laugh", group=live)
love = Gtk.CheckButton(label="Love", group=live)
var live = new Gtk.CheckButton.with_label ("Live");
var laugh = new Gtk.CheckButton.with_label ("Laugh");
var love = new Gtk.CheckButton.with_label ("Love");
laugh.group = live;
love.group = live;
const live = new Gtk.CheckButton({ label: "Live" });
const laugh = new Gtk.CheckButton({ label: "Laugh", group: live });
const love = new Gtk.CheckButton({ label: "Love", group: live });
<object class="GtkBox">
<child>
<object class="GtkCheckButton" id="live">
<property name="label">Live</property>
</object>
</child>
<child>
<object class="GtkCheckButton" id="laugh">
<property name="label">Laugh</property>
<property name="group">live</property>
</object>
</child>
<child>
<object class="GtkCheckButton" id="love">
<property name="label">Love</property>
<property name="group">live</property>
</object>
</child>
</object>
检测被激活的按钮¶
您可以使用“toggled”信号,或者可以使用“active”属性。
static void
on_toggled (GtkCheckButton *button,
const char *identifier)
{
gboolean is_active = gtk_check_button_get_active (button);
if (strcmp (identifier, "live") == 0)
update_live (is_active); // update_live() is defined elsewhere
else if (strcmp (identifier, "laugh") == 0)
update_laugh (is_active); // update_laugh() is defined elsewhere
else if (strcmp (identifier, "love") == 0)
update_love (is_active); // update_love() is defined elsewhere
}
// ...
// The live, laugh, and love variables are defined like the example above
g_signal_connect (live, "toggled", G_CALLBACK (on_toggled), "live");
g_signal_connect (laugh, "toggled", G_CALLBACLK (on_toggled), "laugh");
g_signal_connect (love, "love", G_CALLBACK (on_toggled), "love");
def on_toggled(button, identifier):
is_active = button.props.active
if identifier == "live":
# update_live() is defined elsewhere
update_live(is_active)
elif identifier == "laugh":
# update_laugh() is defined elsewhere
update_laugh(is_active)
elif identifier == "love":
# update_love() is defined elsewhere
update_love(is_active)
# The live, laugh, and love variables are defined like the example above
live.connect("toggled", on_toggled, "live")
laugh.connect("toggled", on_toggled, "laugh")
love.connect("toggled", on_toggled, "love")
组件的常用方法¶
如果您想为您的单选按钮启用助记符快捷键,可以使用
new_with_mnemonic()构造函数,或者set_use_underline()方法。
API 参考¶
在示例中,我们使用了以下类