以下gtk+编程例子是来自书籍《实用技术:开发Linux应用——用GTK+和GDK开发Linux图形用户界面应用》第40页的内容——组合网格
在openSUSE-Leap-15.3-DVD-x86_64的gnome3.34.7桌面环境下编译的,其中pkg-config --cflags --libs gtk±3.0两边有反引号括起来的
ruhong@localhost:~/gtk> gcc page4002.c -o page4002 pkg-config --cflags --libs gtk±3.0
ruhong@localhost:~/gtk> ./page4002
因为table已经被grid替代了,以下例子纯粹是个人仿照写
/* the example on page 40 */
#include <gtk/gtk.h>
gint Delete (GtkWidget *widget, gpointer gdata)
{
gtk_main_quit ();
return (FALSE);
}
int main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *grid;
GtkWidget *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "test_grid");
g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (Delete), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 100);
grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid);
gtk_widget_show (grid);
button = gtk_button_new_with_label ("button1");
gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
gtk_widget_show (button);
button = gtk_button_new_with_label ("button2");
gtk_grid_attach (GTK_GRID (grid), button, 1, 0, 2, 1);
gtk_widget_show (button);
button = gtk_button_new_with_label ("button3");
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 1, 2);
gtk_widget_show (button);
button = gtk_button_new_with_label ("button4");
gtk_grid_attach (GTK_GRID (grid), button, 1, 1, 2, 1);
gtk_widget_show (button);
button = gtk_button_new_with_label ("button5");
gtk_grid_attach (GTK_GRID (grid), button, 1, 2, 1, 1);
gtk_widget_show (button);
button = gtk_button_new_with_label ("button6");
gtk_grid_attach (GTK_GRID (grid), button, 2, 2, 1, 1);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}
效果图如下
button1的起点是0,0,x轴增加1个单位,y轴向下增加1个单位
button2的起点是1,0,x轴增加2个单位,y轴向下增加1个单位
button3的起点是0,1,x轴增加1个单位,y轴向下增加2个单位
button4的起点是1,1,x轴增加2个单位,y轴向下增加1个单位
button5的起点是1,2,x轴增加1个单位,y轴向下增加1个单位
button6的起点是2,2,x轴增加1个单位,y轴向下增加1个单位
2022-03-27补充更改,用手机偶然看到别人的博客https://blog.csdn.net/weixin_44374594/article/details/117720479得到的启示,就将
gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE);
gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE);
这两个函数加入到书中的例子,意外能实现与书中图片一样的效果,真有茅塞顿开的感觉,而以前经过几次测试没有达到效果,就使用gtk_grid_attach_next_to才实现
/* the example on page 4004 */
#include <gtk/gtk.h>
/* 准备材料,自定义函数 */
/* comments注释 */
/* 程序主体 */
int main( int argc, char *argv[] )
{
GtkWidget *window;
/* 声明变量window为构件gtkwidget */
GtkWidget *button;
GtkWidget *grid;
gtk_init (&argc, &argv);
/* 初始化gtk */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* 创建顶级窗口 */
gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);
/* 设置窗口的大小,横为1200像素,竖为900像素 */
gtk_window_set_title (GTK_WINDOW (window), "对比table和grid");
/* 为窗口顶部加上标题为对比table和grid */
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
/* 为窗口加上动作,当收到destroy信号,就启动gtk_main_quit事件,gtk_main_quit就是鼠标单击窗口右上角的关闭按钮就终止程序 */
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
/* 简单的理解是将窗口变成容器,可以装入其它构件,构件边缘与窗口边缘的距离为10像数 */
grid = gtk_grid_new ();
/* 创建网格 */
gtk_container_add (GTK_CONTAINER(window), grid);
/* 将网格装入窗口容器 */
gtk_grid_set_row_homogeneous (GTK_GRID (grid), TRUE);
gtk_grid_set_column_homogeneous (GTK_GRID (grid), TRUE);
/* homogeneous是同质的, 均匀的意思,将网格内的所有按钮统一使用一个标准尺寸 */
button = gtk_button_new_with_label ("Button at 0,0");
gtk_grid_attach (GTK_GRID(grid), button, 0, 0, 1, 1);
button = gtk_button_new_with_label ("Big square button");
gtk_grid_attach (GTK_GRID (grid), button, 2, 2, 2, 2);
/* 将网格装入窗口容器 */
button = gtk_button_new_with_label ("Short fat button");
/* 创建按钮并带有标签Short fat button */
gtk_grid_attach (GTK_GRID (grid), button, 0, 3, 2, 1);
/* 横线起点是0,向右增加2个格位,竖线起点是3,向下增加1个格位 */
button = gtk_button_new_with_label ("Tall skinny button");
gtk_grid_attach (GTK_GRID (grid), button, 3, 0, 1, 2);
gtk_widget_show_all (window);
/* 显示构件窗口包括所有构件 */
gtk_main ();
/* 进入主循环,等待信号(鼠标键盘)输入 */
return 0;
}