gtk+blade+anjuta 的简单实例我的系统 ubuntu 12.04
不正确会有很多问题,速度慢,找不到安装的软件。163的源就不错如页面提示操作后,apt-get install update ; apt-get install upgrade
2> 安装软件
3> 简单测试apt-get install build-essential #据说包含包含 一些基本工具,linux机器一般都具有apt-get install gcc make gdb #安装编译工具apt-get install gnome-core-devel #真大啊,400M+,比较懒的做法,把gnome的开发核心一下子装上,gtk只是它 的子集apt-get install devhelp #帮助文档apt-get install glade #界面设计工具apt-get install anjuta #gta 最著名的IDE
c代码#includevoid hello(GtkWidget *widget,gpointer data){g_print("Hello Ubuntu!\n");}gint delete_event(GtkWidget *widget,GdkEvent *event,gpointer data){g_print ("delete event occurred\n");return(TRUE);}void destroy(GtkWidget *widget,gpointer data){gtk_main_quit();}int main( int argc, char *argv[] ){GtkWidget *window;GtkWidget *button;gtk_init (&argc, &argv);window=gtk_window_new (GTK_WINDOW_TOPLEVEL);gtk_signal_connect (GTK_OBJECT(window),"delete_event",GTK_SIGNAL_FUNC(delete_event),NULL);gtk_signal_connect (GTK_OBJECT (window), "destroy",GTK_SIGNAL_FUNC (destroy), NULL);gtk_container_set_border_width (GTK_CONTAINER (window), 10);button = gtk_button_new_with_label ("Hello Ubuntu!");gtk_signal_connect (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (hello), NULL);gtk_signal_connect_object (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (gtk_widget_destroy),GTK_OBJECT (window));gtk_container_add (GTK_CONTAINER (window), button);gtk_widget_show (button);gtk_widget_show (window); /*显示一个窗口*/gtk_main(); /*进入主循环*/return(0);}编译
运行gcc test.c -o gtktest `pkg-config --cflags --libs gtk+-2.0`
测试成功./gtktest
出现个gtktest为title的窗口,窗口中只有个“Hello Ubuntu!”的按钮
4> glade+gtk测试
glade 操作打开glade,创建个工程,实际就是生成一个文件,默认是glade为扩展名的xml格式的文件保存为 test.glade1: 添加个窗口在“常规“(general)面板中设置窗口的属性“名称”(name)中写入窗口ID,默认值为 window1, 源码实例化时会用到2: 触发gtk中的函数在“信号”(signal)面板中设置触发为简单起,只在窗口销毁时触发我们的函数,gtk销毁的信号是在GtkWidget中继承的,所以在“GtkWidget”的树形结构中设置“处理函数”的值:选中"destroy"信号,在“处理函数”列中设置值: on_windown1_destroy. 这个值其实就是在gtk中的源码中调用的为做测试,在添加个,一个信号可以设置多个选中"destroy"信号,在“处理函数”列中设置值: on_windown1_destroy_my
生成的文件: test.gladewindow1">Falsew_titleon_window1_destroy" swapped="no"/>on_window1_destroy_my" swapped="no"/>3: c源码:test.c#include#define DBG(s) printf("%-30s %04d %s\n",__func__, __LINE__, (s))voidon_window1_destroy (GtkObject *object, gpointer user_data){DBG("------");gtk_main_quit ();}voidon_window1_destroy_my (GtkObject *object, gpointer user_data){DBG("------");gtk_main_quit ();}intmain (int argc, char *argv[]){GtkBuilder *builder;GtkWidget *window;gtk_init (&argc, &argv);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);g_object_unref (G_OBJECT (builder));DBG("------");gtk_widget_show (window);DBG("------");gtk_main ();DBG("------");return 0;}编译
运行gcc -Wall -g -o test test.c `pkg-config --cflags --libs gtk+-2.0` -export-dynamic
./test结果
总结出现窗口,点击关闭,窗口退出终端显示main 0033 ------main 0036 ------on_window1_destroy 0008 ------on_window1_destroy_my 0014 ------main 0039 ------
glade生成xml。c使用GtkBuilder生成个对象,builder。builder通过检索xml中的 ”id“, 生成图形对象,检索xml中的”handler“,关联到C中的函数
启动anjuta -> 新建项目 -> 选择项目类型:”GTK+简单“ -> 基本信息:
继续 -> 默认值,不更改,一直回到主界面。默认项目名称,gtk-foobar,不更改
执行
就会出现个空白窗口。看看Anjuta为我们生成了什么
├── autom4te.cache├── Debug│ ├── po│ └── src├── po└── src源码文件:src/*Debug生成的文件: Debug/*其他的都是项目管理文件最重要的是src文件
ls src
gtk_foobar.ui main.c Makefile.am Makefile.in
c源码:main.ccat src/main.c/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- *//** main.c* Copyright (C) 2012 root** gtk-foobar is free software: you can redistribute it and/or modify it* under the terms of the GNU General Public License as published by the* Free Software Foundation, either version 3 of the License, or* (at your option) any later version.** gtk-foobar is distributed in the hope that it will be useful, but* WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.* See the GNU General Public License for more details.** You should have received a copy of the GNU General Public License along* with this program. If not, see .*/#include#include#includetypedef struct _Private Private;static struct _Private{/* ANJUTA: Widgets declaration for gtk_foobar.ui - DO NOT REMOVE */};static struct Private* priv = NULL;/* For testing propose use the local (not installed) ui file *//* #define UI_FILE PACKAGE_DATA_DIR"/ui/gtk_foobar.ui" */#define UI_FILE "src/gtk_foobar.ui"#define TOP_WINDOW "window"/* Signal handlers *//* Note: These may not be declared static because signal autoconnection* only works with non-static methods*//* Called when the window is closed */voiddestroy (GtkWidget *widget, gpointer data){gtk_main_quit ();}static GtkWidget*create_window (void){GtkWidget *window;GtkBuilder *builder;GError* error = NULL;/* Load UI from file */builder = gtk_builder_new ();if (!gtk_builder_add_from_file (builder, UI_FILE, &error)){g_critical ("Couldn't load builder file: %s", error->message);g_error_free (error);}/* Auto-connect signal handlers */gtk_builder_connect_signals (builder, NULL);/* Get the window object from the ui file */window = GTK_WIDGET (gtk_builder_get_object (builder, TOP_WINDOW));if (!window){g_critical ("Widget \"%s\" is missing in file %s.",TOP_WINDOW,UI_FILE);}priv = g_malloc (sizeof (struct _Private));/* ANJUTA: Widgets initialization for gtk_foobar.ui - DO NOT REMOVE */g_object_unref (builder);return window;}intmain (int argc, char *argv[]){GtkWidget *window;#ifdef ENABLE_NLSbindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");textdomain (GETTEXT_PACKAGE);#endifgtk_init (&argc, &argv);window = create_window ();gtk_widget_show (window);gtk_main ();g_free (priv);return 0;}ui源码文件: gtk_foobar.uiwindow">Truewindow500400destroy"/>我用不同的颜色标识他们的区别与联系和上面我们自己用glade+gtk例子手动完成的几乎一样。
通过后面的几个测试,就对gtk的开发有个初步认识。
Micah 2007年 《GTK+ and Glade3 GUI Programming Tutorial》
某热心人对上面的翻译