Linux 下從頭再走 GTK+-3.0 (五),gtk-3.0

來源:互聯網
上載者:User

Linux 下從頭再走 GTK+-3.0 (五),gtk-3.0

  實踐中表明,純粹利用 gtk 函數來建立 UI 是很繁瑣的事,需要編寫很多代碼。怎樣才能快速統一的建立 UI 布局呢?

可喜的是 GTK 提供了一個 GtkBuilder 用於快速建立介面。它讀取一個 xml 檔案。按照 xml 檔案中所描述的來建立介面。因此我們需要編寫 xml 檔案。

建立example.ui ,內容如下:

<?xml version="1.0" encoding="UTF-8"?><interface>    <object id="window" class="GtkWindow">        <property name="visible">True</property>        <property name="title">GtkBuilder</property>        <property name="border-width">30</property>        <child>            <object id="grid" class="GtkGrid">                <property name="visible">True</property>                <child>                    <object id="print_hello" class="GtkButton">                        <property name="visible">True</property>                        <property name="label">Button 1</property>                    </object>                    <packing>                        <property name="left-attach">0</property>                        <property name="top-attach">0</property>                    </packing>                </child>                <child>                    <object id="print_entry" class="GtkButton">                        <property name="visible">True</property>                        <property name="label">Button 2</property>                    </object>                    <packing>                        <property name="left-attach">1</property>                        <property name="top-attach">0</property>                    </packing>                </child>                <child>                    <object id="entry" class="GtkEntry">                        <property name="visible">True</property>                    </object>                    <packing>                        <property name="left-attach">0</property>                        <property name="top-attach">1</property>                        <property name="width">2</property>                    </packing>                </child>            </object>            <packing>            </packing>        </child>    </object></interface>

再建立源檔案 example.c  ,內容如下:

#include <gtk/gtk.h>static void print_hello(GtkWidget *button , gpointer data);static void activate(GtkApplication *app , gpointer data);static void print_entry(GtkWidget * button , gpointer entry);int main(int argc , char **argv){    GtkApplication *app;    int app_status;    app = gtk_application_new("org.rain.example" , G_APPLICATION_FLAGS_NONE);    g_signal_connect(app , "activate" , G_CALLBACK(activate) , NULL);    app_status = g_application_run(G_APPLICATION(app) , argc , argv);    g_object_unref(app);    return app_status;}static void print_hello(GtkWidget *button , gpointer data){    printf("Hello,world!\n");}static void print_entry(GtkWidget * button , gpointer entry){    const gchar *entry_buf;    //擷取輸入框的內容,返回一個 const gchar 指標 , gchar 即是 char    entry_buf = gtk_entry_get_text( GTK_ENTRY(entry) );    if('\0' != entry_buf[0])        printf("%s\n" , entry_buf);}static void activate(GtkApplication *app , gpointer data){    GtkBuilder *builder;    GObject *window;    GObject *button;    GObject *entry;    //建立一個 GtkBuilder 。    builder = gtk_builder_new();    //將描述 UI 的檔案添加到 Builder 中。    gtk_builder_add_from_file(builder , "example.ui" , NULL);    //可以用該函數擷取 Builder 建立的對象,根據 example.ui 檔案中設定的 object 的 id 來擷取。    window = gtk_builder_get_object(builder , "window");    gtk_application_add_window(app , GTK_WINDOW(window));    //注意該函數返回的是一個指向 GObject 對象的指標。    button = gtk_builder_get_object(builder , "print_hello");    //設定按鈕標籤    gtk_button_set_label ( GTK_BUTTON (button) , "PrintHello");    g_signal_connect(button , "clicked" , G_CALLBACK(print_hello) , NULL);    //擷取輸入框對象。    entry  = gtk_builder_get_object(builder , "entry");        button = gtk_builder_get_object(builder , "print_entry");    gtk_button_set_label ( GTK_BUTTON (button) , "PrintEntry");    //調用 print_entry 時傳遞使用者參數 entry 。    g_signal_connect(button , "clicked" , G_CALLBACK(print_entry) , entry);    }

編譯,運行:

gcc example.c `pkg-config --cflags --libs gtk+-3.0`./a.out 

運行結果如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.