前面给大家提过,用windows版本glade可以 2 种格式的glade: Libglade ( gtk2.12前的版本 ), GtkBuilder( 新版的,现在用的是这个版本 )。格式不一样,最终使用的库函数不一样。如图:
1)glade画UI,注意保存为Libglade格式,如下图:
c代码如下:
#include <glade/glade.h>
#include <gtk/gtk.h>
void on_click_button(GtkButton* button,gpointer data)
{
printf("欢迎来到凌阳教育!\n");
}
int main(int argc,char **argv)
{
GladeXML *gxml;
GtkWidget *window = NULL, *button = NULL;
gtk_init(&argc,&argv);
//下面开始通过文件获取布局信息了
gxml=glade_xml_new("libglade.glade",NULL,NULL);
//获取控件
window=glade_xml_get_widget(gxml,"window1"); /* window1 是glade3 中窗口的名字*/
button = glade_xml_get_widget(gxml,"button1");
g_signal_connect(button, "clicked", on_click_button, NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
在编译代码时,加上 libglade-2.0,如:
gcc demo.c -o demo `pkg-config --cflags --libs gtk+-2.0 libglade-2.0`
2)glade画UI,注意保存为GtkBuilder格式,如下图:
c代码如下:
#include <gtk/gtk.h>
void on_click_button(GtkButton *button, gpointer data)
{
printf("欢迎来到凌阳教育!\n");
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window = NULL, *button = NULL;
GtkBuilder *builder = gtk_builder_new();
if(!gtk_builder_add_from_file(builder, "./gtk_builder.glade", NULL))
printf("cannot load file!");
window = GTK_WIDGET(gtk_builder_get_object (builder, "window1"));
button = GTK_WIDGET(gtk_builder_get_object (builder, "button1"));
g_signal_connect(button, "clicked", on_click_button, NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}