文字描述不出来 所以直接上图:
将文件保存为spin.glade 在code::blocks里面输入以下代码: /* 作者:bailangcn 时间:2012/3/23 作者主页:bailangcn.tk */ #include <gtk/gtk.h>
GtkWidget *label1;
GtkWidget *label2;
GtkWidget *spin;
GtkWidget *combobox;
void set_label1(GtkWidget *widget,gpointer data)
{
gint text;
const char buf[6];
text=gtk_spin_button_get_value_as_int((GTK_SPIN_BUTTON(spin)));
sprintf(buf,"%d",text);
gtk_label_set_text(GTK_LABEL(label1),buf);
}
void set_label2(GtkWidget *widget,gpointer *data)
{
const char *text;
text=gtk_combo_box_get_active_text(GTK_COMBO_BOX(combobox));
gtk_label_set_text(GTK_LABEL(label2),text);
}
int main(int argc, char *argv[])
{
GtkBuilder *builder;
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *button1;
GtkWidget *button2; builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "spin.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
spin = GTK_WIDGET(gtk_builder_get_object(builder,"spinbutton"));
combobox = GTK_WIDGET(gtk_builder_get_object(builder,"combobox"));
button1 = GTK_WIDGET(gtk_builder_get_object(builder,"button1"));
button2 = GTK_WIDGET(gtk_builder_get_object(builder,"button2"));
label1 = GTK_WIDGET(gtk_builder_get_object(builder,"label1"));
label2 = GTK_WIDGET(gtk_builder_get_object(builder,"label2"));
gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0);
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(G_OBJECT(button1),"pressed",G_CALLBACK(set_label1),NULL);
g_signal_connect(G_OBJECT(button2),"pressed",G_CALLBACK(set_label2),NULL); // gtk_builder_connect_signals(builder, NULL);//自动关联已在 UI 文件配置好的信号 如果你不在galde文件里面配置信号这行代码可以不用 g_object_unref(G_OBJECT(builder));
gtk_widget_show_all(window);
gtk_main();
return 0;
}
运行如图所示:
|