使用Glade进行GTK开发

在前面的文章介绍了使用Glade进行PyGTK开发,现在再来说一说使用Glade来进行GTK的开发。

首先需要的开发环境:
Glade3、GTK+2.0、GCC

先用Glade设计一个用户界面,保存为test.glade,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="resizable">False</property>
<property name="default_width">440</property>
<property name="default_height">450</property>
<signal name="destroy" handler="on_MainWindow_destroy" swapped="no"/>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="top_padding">10</property>
<property name="bottom_padding">10</property>
<property name="left_padding">10</property>
<property name="right_padding">10</property>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">龙昌博客: http://www.xefan.com</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">关闭</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_MainWindow_destroy" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>

再创建一个C源文件,保存为test.c,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <gtk/gtk.h>
void on_MainWindow_destroy(GtkObject *widget, gpointer data)
{
gtk_main_quit();
}
int main(int argc, char *argv[])
{
GtkBuilder *builder;
gtk_init(&argc, &argv);
GtkWidget *window;
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "test.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
gtk_builder_connect_signals(builder, NULL);
// 释放builder的空间
g_object_unref(G_OBJECT(builder));
gtk_widget_show_all(window);
gtk_main();
return 0;
}

然后再编译运行查看效果。
如果出现类似“Gtk-WARNING **: Could not find signal handler ‘on_MainWindow_destroy’”的错误请尝试以下方法:
方法一:将编译命令改为:gcc -o test test.c `pkg-config –libs –cflags gtk+-2.0` -export-dynamic
方法二:将编译命令改为:gcc -o test test.c `pkg-config –libs –cflags libglade-2.0 gmodule-export-2.0`