GTK+容器管理器–table

#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
GtkWidget* window;
GtkWidget* table;

GtkWidget* button[4][4];
char* button_title[4][4] = { { "7", "8", "9", "/" },
{ "4", "5", "6", "*" }, { "1", "2", "3", "-" }, { "0", ".", "=",
"+" } };

unsigned int i, j;

//设置主窗体
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "测试容器管理--Table");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
gtk_container_set_border_width(GTK_CONTAINER(window), 20);//窗体边框4

g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

//设置table容器布局管理器
table = gtk_table_new(4, 4, TRUE);//4行4列
gtk_table_set_row_spacings(GTK_TABLE(table), 2);//所有行的子元素按钮距离2
gtk_table_set_col_spacings(GTK_TABLE(table), 2);//所有列的子元素按钮距离2

//添加按钮
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
button[i][j] = gtk_button_new_with_label(button_title[i][j]);
gtk_table_attach_defaults(GTK_TABLE(table), button[i][j], j, j + 1,
i, i + 1);
}
}

gtk_container_add(GTK_CONTAINER(window), table);

//最终显示
gtk_widget_show_all(window);
gtk_main();
return 0;
}

Leave a Reply

Your email address will not be published.