问题
This is my codes:
#include<gtk/gtk.h>
int main(int argc,char**argv)
{
GtkWidget* window, *button, *grid;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window),100,100);
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
//gtk_container_set_border_width(GTK_CONTAINER(window),10);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
grid = gtk_grid_new();
button = gtk_button_new_with_label("1");
gtk_grid_attach(GTK_GRID(grid), button, 0,0, 100, 1);
gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE);
gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE);
button = gtk_button_new_with_label("1");
gtk_grid_attach(GTK_GRID(grid), button,0, 1, 10, 1);
gtk_container_add(GTK_CONTAINER(window), grid);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
the result of this codes is : gtk_grid_attach(GTK_GRID(grid), button,0, 1, 100, 1)
When I change gtk_grid_attach(GTK_GRID(grid), button,0, 1, 100, 1) to gtk_grid_attach(GTK_GRID(grid), button,0, 1, 200, 1) Then the result of this code is: gtk_grid_attach(GTK_GRID(grid), button,0, 1, 200, 1)
Why does the width of window increase? I find that the size of window may be varied with the function gtk_grid_attach(). So how to solve it?
回答1:
Why does the width of window increase?
You have used
gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE);
gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE);
Which means that each cell in the grid will have equal width and height, which will be the same as the largest widget embedded. So If you have more cells, the grid will grow further and thus the window width increases
I find that the size of window may be varied with the function gtk_grid_attach(). So how to solve it?
I don't know what's your usecase is. But you could unset the homogeneous properties and/or use a real grid width and height when attaching children.
来源:https://stackoverflow.com/questions/62042389/how-to-prevent-gtkwindow-growing