打开glade3 新建一个窗口,在窗口中放置一个固定fixed然后再放置三个检查按钮 三个单选按钮 放置一个双态按钮 然后分别在检查按钮 单选按钮 双态按钮下面分别放置一个标签 并且将单选按钮2 单选按钮3 加入单选按钮1的组别中 保存为button.glade 如图所示:
然后打开code::blocks 输入以下代码 代码比较简单就不加注释了
/*
作者:bailangcn
时间:2012/3/19
作者主页:bailangcn.tk
*/
#include <gtk/gtk.h>
GtkWidget *label1;
GtkWidget *label2;
GtkWidget *label3;
void set_text_to_label1(GtkWidget *Widget,gpointer *data)
{
gtk_label_set_text(GTK_LABEL(label1),(char *)data);
}
void set_text_to_label2(GtkWidget *Widget,gpointer *data)
{
gtk_label_set_text(GTK_LABEL(label2),(char *)data);
}
void set_text_to_label3(GtkWidget *Widget,gpointer *data)
{
gtk_label_set_text(GTK_LABEL(label3),(char *)data);
}
int main(int argc, char *argv[])
{
GtkBuilder *builder;
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *checkbutton1;
GtkWidget *checkbutton2;
GtkWidget *checkbutton3;
GtkWidget *radiobutton1;
GtkWidget *radiobutton2;
GtkWidget *radiobutton3;
GtkWidget *togglebutton;
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "button.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
checkbutton1 = GTK_WIDGET(gtk_builder_get_object(builder,"checkbutton1"));
checkbutton2 = GTK_WIDGET(gtk_builder_get_object(builder,"checkbutton2"));
checkbutton3 = GTK_WIDGET(gtk_builder_get_object(builder,"checkbutton3"));
radiobutton1 = GTK_WIDGET(gtk_builder_get_object(builder,"radiobutton1"));
radiobutton2 = GTK_WIDGET(gtk_builder_get_object(builder,"radiobutton2"));
radiobutton3 = GTK_WIDGET(gtk_builder_get_object(builder,"radiobutton3"));
togglebutton = GTK_WIDGET(gtk_builder_get_object(builder,"togglebutton"));
label1 = GTK_WIDGET(gtk_builder_get_object(builder,"label1"));
label2 = GTK_WIDGET(gtk_builder_get_object(builder,"label2"));
label3 = GTK_WIDGET(gtk_builder_get_object(builder,"label3"));
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(G_OBJECT(checkbutton1),"pressed",G_CALLBACK(set_text_to_label1),"you check checkbutton1");
g_signal_connect(G_OBJECT(checkbutton2),"pressed",G_CALLBACK(set_text_to_label1),"you check checkbutton2");
g_signal_connect(G_OBJECT(checkbutton3),"pressed",G_CALLBACK(set_text_to_label1),"you check checkbutton3");
g_signal_connect(G_OBJECT(radiobutton1),"pressed",G_CALLBACK(set_text_to_label2),"you check radiobutton1");
g_signal_connect(G_OBJECT(radiobutton2),"pressed",G_CALLBACK(set_text_to_label2),"you check radiobutton2");
g_signal_connect(G_OBJECT(radiobutton3),"pressed",G_CALLBACK(set_text_to_label2),"you check radiobutton3");
g_signal_connect(G_OBJECT(togglebutton),"toggled",G_CALLBACK(set_text_to_label3),"toggle");
// gtk_builder_connect_signals(builder, NULL);//自动关联已在 UI 文件配置好的信号 如果你不在galde文件里面配置信号这行代码可以不用
g_object_unref(G_OBJECT(builder));
gtk_widget_show_all(window);
gtk_main();
return 0;
}
编译运行 点击不同按钮 会输出不同的文字
如图:
以上对于按钮的回调仅用来演示 实际运用当中 应如用以下代码判断
if (GTK_TOGGLE_BUTTON (widget)->active)
{
/* 如果执行这段程序,表明按钮是选中或者按下的* /
} else {
/* 如果执行这段程序,按钮是没有选中或者弹起的* /
}