如何将小部件置于网格-GTK C中

发布于 2025-02-11 05:21:33 字数 1010 浏览 2 评论 0原文

我正在尝试将这些按钮集中在此网格中,但是我尝试了SET_HEXPAND和变体,而没有什么是我想要的。

GtkWidget *p_main_grid = gtk_grid_new();

   gtk_grid_set_column_spacing(GTK_GRID(p_main_grid), 16);
   gtk_grid_set_row_spacing(GTK_GRID(p_main_grid), 16);

   /* Buttons */

   GtkWidget *p_button_1 = gtk_button_new_with_label("Button 1");
   GtkWidget *p_button_2 = gtk_button_new_with_label("Button 2");

   /* Add Buttons to grid */

   gtk_grid_attach(GTK_GRID(p_main_grid), p_button_1, 5, 3, 1, 1);
   gtk_grid_attach_next_to(GTK_GRID(p_main_grid), p_button_2, p_button_1,
                           GTK_POS_RIGHT, 1, 1);

   /* Add grid to main window */

   gtk_window_set_child(GTK_WINDOW(p_main_window), p_main_grid);
   gtk_window_present(GTK_WINDOW(p_main_window));

我在linux上,使用gtk4

”在此处输入图像说明”

I'm trying to center these buttons in a this grid but I've tried set_hexpand and variants and nothing's what I'm looking for.

GtkWidget *p_main_grid = gtk_grid_new();

   gtk_grid_set_column_spacing(GTK_GRID(p_main_grid), 16);
   gtk_grid_set_row_spacing(GTK_GRID(p_main_grid), 16);

   /* Buttons */

   GtkWidget *p_button_1 = gtk_button_new_with_label("Button 1");
   GtkWidget *p_button_2 = gtk_button_new_with_label("Button 2");

   /* Add Buttons to grid */

   gtk_grid_attach(GTK_GRID(p_main_grid), p_button_1, 5, 3, 1, 1);
   gtk_grid_attach_next_to(GTK_GRID(p_main_grid), p_button_2, p_button_1,
                           GTK_POS_RIGHT, 1, 1);

   /* Add grid to main window */

   gtk_window_set_child(GTK_WINDOW(p_main_window), p_main_grid);
   gtk_window_present(GTK_WINDOW(p_main_window));

I'm on linux and using gtk4

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2025-02-18 05:21:33

我在gtk3中使用了它,并以两个按钮为中心。
这试图使用10列和10行。
16的间距似乎在按钮之间提供了一定的距离。

    grid = gtk_grid_new ( );
    gtk_grid_set_column_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_row_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_column_homogeneous ( GTK_GRID ( grid), TRUE);
    gtk_grid_set_row_homogeneous ( GTK_GRID ( grid), TRUE);

    gtk_container_add ( GTK_CONTAINER (window), grid);

    //blank label to take up 2 cols by 10 rows on left
    title = gtk_label_new ( "");
    gtk_grid_attach (GTK_GRID ( grid), title, 0, 0, 2, 10);

    //blank lable to take up 6 cols by 4 rows at top center
    title = gtk_label_new ( "");
    gtk_grid_attach (GTK_GRID ( grid), title, 2, 0, 6, 4);

    //buttons 3 cols by 1 row each placed below the top center blank label
    button1 = gtk_button_new_with_label ( "button 1");
    gtk_grid_attach (GTK_GRID ( grid), button1, 2, 4, 3, 1);
    button2 = gtk_button_new_with_label ( "button 2");
    gtk_grid_attach (GTK_GRID ( grid), button2, 5, 4, 3, 1);

    //blank label to take up 2 cols by 10 rows on right
    title = gtk_label_new ( "");
    gtk_grid_attach (GTK_GRID ( grid), title, 8, 0, 2, 10);

这与4乘3行一起工作

    gtk_window_set_default_size ( GTK_WINDOW ( window), 400, 200);

    grid = gtk_grid_new ( );
    gtk_grid_set_column_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_row_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_column_homogeneous ( GTK_GRID ( grid), TRUE);
    gtk_grid_set_row_homogeneous ( GTK_GRID ( grid), TRUE);

    gtk_container_add ( GTK_CONTAINER (window), grid);

    title = gtk_label_new ( "");
    //blank label to take up 2 cols by 10 rows on left
    // gtk_grid_attach (GTK_GRID ( grid), title, 0, 0, 2, 10);
    //blank label to take up 1 cols by 3 rows on left
    gtk_grid_attach (GTK_GRID ( grid), title, 0, 0, 1, 3);

    title = gtk_label_new ( "");
    //blank lable to take up 6 cols by 4 rows at top center
    // gtk_grid_attach (GTK_GRID ( grid), title, 2, 0, 6, 4);
    //blank lable to take up 2 cols by 1 rows at top center
    gtk_grid_attach (GTK_GRID ( grid), title, 1, 0, 2, 1);

    //buttons 3 cols by 1 row each placed below the top center blank label
    button1 = gtk_button_new_with_label ( "button 1");
    // gtk_grid_attach (GTK_GRID ( grid), button1, 2, 4, 3, 1);
    //buttons 1 cols by 1 row each placed below the top center blank label
    gtk_grid_attach (GTK_GRID ( grid), button1, 1, 1, 1, 1);
    button2 = gtk_button_new_with_label ( "button 2");
    // gtk_grid_attach (GTK_GRID ( grid), button2, 5, 4, 3, 1);
    gtk_grid_attach (GTK_GRID ( grid), button2, 2, 1, 1, 1);

    title = gtk_label_new ( "");
    //blank label to take up 2 cols by 10 rows on right
    // gtk_grid_attach (GTK_GRID ( grid), title, 8, 0, 2, 10);
    //blank label to take up 1 cols by 3 rows on right
    gtk_grid_attach (GTK_GRID ( grid), title, 3, 0, 1, 3);

I used this in gtk3 and it centered two buttons.
This tries to use 10 columns and 10 rows.
The spacing of 16 seems to provide some distance between the buttons.

    grid = gtk_grid_new ( );
    gtk_grid_set_column_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_row_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_column_homogeneous ( GTK_GRID ( grid), TRUE);
    gtk_grid_set_row_homogeneous ( GTK_GRID ( grid), TRUE);

    gtk_container_add ( GTK_CONTAINER (window), grid);

    //blank label to take up 2 cols by 10 rows on left
    title = gtk_label_new ( "");
    gtk_grid_attach (GTK_GRID ( grid), title, 0, 0, 2, 10);

    //blank lable to take up 6 cols by 4 rows at top center
    title = gtk_label_new ( "");
    gtk_grid_attach (GTK_GRID ( grid), title, 2, 0, 6, 4);

    //buttons 3 cols by 1 row each placed below the top center blank label
    button1 = gtk_button_new_with_label ( "button 1");
    gtk_grid_attach (GTK_GRID ( grid), button1, 2, 4, 3, 1);
    button2 = gtk_button_new_with_label ( "button 2");
    gtk_grid_attach (GTK_GRID ( grid), button2, 5, 4, 3, 1);

    //blank label to take up 2 cols by 10 rows on right
    title = gtk_label_new ( "");
    gtk_grid_attach (GTK_GRID ( grid), title, 8, 0, 2, 10);

This is working with 4 cols by 3 rows

    gtk_window_set_default_size ( GTK_WINDOW ( window), 400, 200);

    grid = gtk_grid_new ( );
    gtk_grid_set_column_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_row_spacing(GTK_GRID(grid), 16);
    gtk_grid_set_column_homogeneous ( GTK_GRID ( grid), TRUE);
    gtk_grid_set_row_homogeneous ( GTK_GRID ( grid), TRUE);

    gtk_container_add ( GTK_CONTAINER (window), grid);

    title = gtk_label_new ( "");
    //blank label to take up 2 cols by 10 rows on left
    // gtk_grid_attach (GTK_GRID ( grid), title, 0, 0, 2, 10);
    //blank label to take up 1 cols by 3 rows on left
    gtk_grid_attach (GTK_GRID ( grid), title, 0, 0, 1, 3);

    title = gtk_label_new ( "");
    //blank lable to take up 6 cols by 4 rows at top center
    // gtk_grid_attach (GTK_GRID ( grid), title, 2, 0, 6, 4);
    //blank lable to take up 2 cols by 1 rows at top center
    gtk_grid_attach (GTK_GRID ( grid), title, 1, 0, 2, 1);

    //buttons 3 cols by 1 row each placed below the top center blank label
    button1 = gtk_button_new_with_label ( "button 1");
    // gtk_grid_attach (GTK_GRID ( grid), button1, 2, 4, 3, 1);
    //buttons 1 cols by 1 row each placed below the top center blank label
    gtk_grid_attach (GTK_GRID ( grid), button1, 1, 1, 1, 1);
    button2 = gtk_button_new_with_label ( "button 2");
    // gtk_grid_attach (GTK_GRID ( grid), button2, 5, 4, 3, 1);
    gtk_grid_attach (GTK_GRID ( grid), button2, 2, 1, 1, 1);

    title = gtk_label_new ( "");
    //blank label to take up 2 cols by 10 rows on right
    // gtk_grid_attach (GTK_GRID ( grid), title, 8, 0, 2, 10);
    //blank label to take up 1 cols by 3 rows on right
    gtk_grid_attach (GTK_GRID ( grid), title, 3, 0, 1, 3);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文