目录
Github
git clone https://github.com/GNOME/glade.git
教程
简介
- Glade是一个用于创建GTK图形用户界面的用户界面构建器。它允许开发者通过可视化方式设计和布局GUI元素,而不必手动编写代码。Glade生成XML格式的描述文件,描述了用户界面的结构和属性。然后,这个XML文件可以由程序加载和解释,从而创建用户界面。
可视化设计
- Glade提供了一个直观的图形用户界面,允许用户通过拖放方式设计和布局界面元素,无需手动编写代码。
生成XML描述文件
- 设计完成后,Glade会生成一个XML格式的描述文件,其中包含了用户界面的结构和属性信息。
与多种编程语言兼容
- 由于生成的描述文件是基于XML的,因此可以与多种编程语言一起使用。GTK支持多种编程语言,包括C、C++、Python等。
与IDE集成
- Glade可以与多个集成开发环境(IDE)一起使用,例如GNOME Builder,以便更方便地进行开发和调试。
安装 Glade
# 目前版本支持gtk+3
brew install glade
glade --version
# 启动
glade
# gtk+3 安装路径
# /usr/local/Cellar/gtk+3/3.24.41
brew info gtk+3
# GTK+3版本号
pkg-config --modversion gtk+-3.0
示例
demo.glade
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkWindow" id="window">
<property name="width-request">400</property>
<property name="height-request">200</property>
<property name="can-focus">False</property>
<property name="title" translatable="yes">demo</property>
<child>
<object class="GtkBox" id="box">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkButton" id="button">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="combobox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
<property name="active">0</property>
<property name="active-id">1</property>
<items>
<item id="1" translatable="yes">item1</item>
<item id="2" translatable="yes">item2</item>
<item id="3" translatable="yes">item3</item>
<item id="4" translatable="yes">item4</item>
</items>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
demo.c
#include <gtk/gtk.h>
static void
on_button_clicked (GtkButton *button, gpointer user_data)
{
g_print ("Hello World\n");
}
static void
on_combobox_changed (GtkComboBox *combobox, gpointer user_data)
{
gint active_item = gtk_combo_box_get_active (combobox);
const gchar *active_id = gtk_combo_box_get_active_id (GTK_COMBO_BOX (combobox));
g_print ("Selected item: %d, active_id: %s\n", active_item, active_id);
}
static void
on_entry_changed (GtkEntry *entry, gpointer user_data)
{
const gchar *text = gtk_entry_get_text (entry);
g_print ("Entry Text: %s\n", text);
}
static void
on_entry_activate (GtkEntry *entry, gpointer user_data)
{
const gchar *text = gtk_entry_get_text (entry);
g_print ("Entry Text: %s\n", text);
}
int
main (int argc, char **argv)
{
GtkBuilder *builder = gtk_builder_new ();
GtkWidget *window;
GtkWidget *button;
GtkWidget *combobox;
GtkWidget *entry;
gtk_init (&argc, &argv);
gtk_builder_add_from_file (builder, "demo.glade", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
button = GTK_WIDGET (gtk_builder_get_object (builder, "button"));
combobox = GTK_WIDGET (gtk_builder_get_object (builder, "combobox"));
entry = GTK_WIDGET (gtk_builder_get_object (builder, "entry"));
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (button, "clicked", G_CALLBACK (on_button_clicked), NULL);
g_signal_connect (combobox, "changed", G_CALLBACK (on_combobox_changed), NULL);
g_signal_connect (entry, "changed", G_CALLBACK (on_entry_changed), NULL);
g_signal_connect (entry, "activate", G_CALLBACK (on_entry_activate), NULL);
g_object_unref (G_OBJECT (builder));
gtk_widget_show (window);
gtk_main ();
return 0;
}
gcc $(pkg-config --cflags gtk+-3.0) -o demo demo.c $(pkg-config --libs gtk+-3.0)
# 或
gcc -o demo demo.c `pkg-config --cflags --libs gtk+-3.0`
标签:10,GTK,builder,gtk,Glade,C语言,entry,True
From: https://www.cnblogs.com/wufengsheng/p/18002862