使用GtkBuilder設計Gtk+界面

火星人 @ 2014-03-12 , reply:0


  作者:linuxeden管理團隊c-aries
Gtk+使用glade進行界面設計能有效地加快項目進度和提高程序的可維護性。
自從gtk2.12,Gtk+已經內建了GtkBuilder,用以代替使用glade編寫的程序所依賴的libglade庫文件。
下面介紹如何使用GtkBuilder寫一個最基本的界面。

1. 使用glade設計Gtk+界面,保存為glade文件
$ ls
fsf.glade  stallman.jpg
$


2. 用gtk-builder-convert腳本(gtk+2.0默認安裝該腳本)將glade代碼轉換成支持GtkBuilder的代碼
$ gtk-builder-convert fsf.glade fsf.ui
Wrote fsf.ui
$ ls
fsf.glade  fsf.ui  stallman.jpg
$ cat fsf.ui
<?xml version="1.0"?>
<!--Generated with glade3 3.4.5 on Sun Nov 29 12:39:11 2009 -->
<interface>
  <object class="GtkWindow" id="window1">
    <child>
      <object class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <child>
          <object class="GtkImage" id="image1">
            <property name="visible">True</property>
            <property name="pixbuf">stallman.jpg</property>
          </object>
        </child>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="label" translatable="yes">Richard Stallman</property>
          </object>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
$

3. 編寫程序,調用GtkBuilder
$ ls
fsf.c  fsf.glade  fsf.ui  Makefile  stallman.jpg
$ cat Makefile
all:
    gcc `pkg-config --cflags --libs gtk+-2.0` fsf.c

clean:
    rm -f *~ a.out
$ cat fsf.c
#include <gtk/gtk.h>

static void window_close(GtkWidget *widget,gpointer data)
{
  gtk_main_quit();
}

int main(int argc,char *argv[])
{
  GtkBuilder *builder;
  GError *error;
  GtkWidget *window;
  gtk_init(&argc,&argv);
  builder=gtk_builder_new();
  gtk_builder_add_from_file(builder,"fsf.ui",&error);
  window=GTK_WIDGET(gtk_builder_get_object(builder,"window1"));
  g_object_unref(G_OBJECT(builder));
  g_signal_connect(window,"destroy",G_CALLBACK(window_close),NULL);
  gtk_widget_show_all(window);
  gtk_main();
  return 0;
}
$


4. 編譯出可執行程序,運行
$ make
gcc `pkg-config --cflags --libs gtk+-2.0` fsf.c
$ ./a.out

效果如下圖:



題外話:
我是在閱讀cheese的源代碼中發現gtkbuilder的,然後又發現了個東西
"pkg-config --cflags --libs gtk+-2.0"這一句是怎麼得到的?
$ ls /usr/lib/pkgconfig/gtk+-2.0.pc
/usr/lib/pkgconfig/gtk+-2.0.pc
$ cat /usr/lib/pkgconfig/gtk+-2.0.pc
prefix=/usr
exec_prefix=${prefix}
libdir=/usr/lib
includedir=${prefix}/include
target=x11

gtk_binary_version=2.10.0
gtk_host=i486-pc-linux-gnu

Name: GTK+
Description: GIMP Tool Kit (${target} target)
Version: 2.12.12
Requires: gdk-${target}-2.0 atk cairo
Libs: -L${libdir} -lgtk-${target}-2.0
Cflags: -I${includedir}/gtk-2.0
$

呵呵,這下明白了吧
比如說編譯調用了gstreamer庫的程序,就可以使用
pkg-config --cflags --libs gstreamer-0.10
pkgconfig的默認目錄/usr/lib/pkgconfig下有gstreamer-0.10.pc這一數據文件
$ ls /usr/lib/pkgconfig/gstreamer-0.10.pc
/usr/lib/pkgconfig/gstreamer-0.10.pc
$


Happy Hacking : )

相關資源:
1. GTK+ programs with GtkBuilder and dynamic signal handlers. | All My Brain
http://allmybrain.com/2008/05/22/gtk-programs-with-gtkbuilder-and-dynamic-signal-handlers/

2. GTK+ Reference Manual -- GtkBuilder
file:///usr/share/doc/libgtk2.0-doc/gtk/GtkBuilder.html

3. GTK+ Reference Manual -- Index
file:///usr/share/doc/libgtk2.0-doc/gtk/ix01.html

注: 2和3的資源,在Debian下運行"sudo apt-get install libgtk2.0-doc"即可獲取

gtk+





[火星人 via ] 使用GtkBuilder設計Gtk+界面已經有169次圍觀

http://www.coctec.com/docs/program/show-post-71629.html