GtkGrid demo4.c

void
gtk_grid_insert_column (GtkGrid *grid, gint position);

void
gtk_grid_insert_row (GtkGrid *grid, gint position);

void
gtk_grid_remove_row (GtkGrid *grid, gint position);

void
gtk_grid_remove_column (GtkGrid *grid, gint position);

void // 使用这个函数来设置相关属性 第一次使用到,记录下
g_object_set (gpointer object,
              const gchar *first_property_name,
              ...);
// demo4.c
#include <gtk/gtk.h>

static GtkWidget *create_window(const gint, const gint);
static void load_css(void);
static GtkWidget *createChild(gchar *, gint, gint, gint, gchar *);

int main(void)
{
    GtkWidget *window;
    GtkWidget *grid;
    GtkWidget *child1;
    GtkWidget *child2;
    GtkWidget *child3;
    GtkWidget *child4;

    gtk_init(NULL, NULL);
    load_css();

    window = create_window(400, 400);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    grid = gtk_grid_new();
    gtk_container_add(GTK_CONTAINER(window), grid);

    child1 = createChild("myChild1", 3, 100, 100, "1");
    child2 = createChild("myChild1", 3, 100, 100, "2");
    child3 = createChild("myChild2", 3, 100, 100, "3");
    child4 = createChild("myChild2", 3, 100, 100, "4");
	
	// ====================测试===========================
    gtk_grid_attach(GTK_GRID(grid), child1, 0, 0, 1, 1);
    gtk_grid_insert_column(GTK_GRID(grid), 0);

    gtk_grid_attach(GTK_GRID(grid), child2, 0, 1, 1, 1);
    gtk_grid_insert_column(GTK_GRID(grid), 0);

    gtk_grid_attach(GTK_GRID(grid), child3, 0, 2, 1, 1);
    gtk_grid_insert_column(GTK_GRID(grid), 0);

    gtk_grid_attach(GTK_GRID(grid), child4, 0, 3, 1, 1);
    // ===================================================

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

static GtkWidget *create_window(const gint w, const gint h)
{
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), w, h);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_title(GTK_WINDOW(window), "Hello Grid");

    return window;
}

static void load_css(void)
{
    GtkCssProvider *provider;
    GdkDisplay *display;
    GdkScreen *screen;

    const char *css_style_file = "style.css";
    GFile *css_fp = g_file_new_for_path(css_style_file);
    GError *error = 0;

    provider = gtk_css_provider_new();
    display = gdk_display_get_default();
    screen = gdk_display_get_default_screen(display);

    gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    gtk_css_provider_load_from_file(provider, css_fp, &error);

    g_object_unref(provider);
}

static GtkWidget *createChild(gchar *name, gint spacing, gint width, gint height, gchar *text)
{

    GtkWidget *label = gtk_label_new(NULL);
    g_object_set(label,
                 "label", text,
                 "name", "myLabel",
                 "margin", 40,
                 "selectable", TRUE, // 先前不怎么懂这个函数,用这个测试了一下,稍微懂了
                 "tooltip-text", text,
                 NULL);

    GtkWidget *child = gtk_grid_new();
    gtk_widget_set_name(child, name);
    gtk_widget_set_size_request(child, width, height);
    gtk_container_set_border_width(GTK_CONTAINER(child), spacing);

    gtk_container_add(GTK_CONTAINER(child), label);
    return child;
}
  • 函数gtk_grid_insert_column(...)的效果:
    最初的效果:
    GtkGrid demo4.c
    使用函数后:

GtkGrid demo4.c

代码解读:我们将child1添加到网格后,使用函数在第一列(0) 插入一列,那么之前的第一列就跑到第二列了,依次类推,可以得到最终的界面效果。

  • 函数gtk_grid_insert_row(...)
// 修改=======之间的代码块===================================
	gtk_grid_attach(GTK_GRID(grid), child4, 0, 0, 1, 1);
    gtk_grid_insert_row(GTK_GRID(grid), 0);

    gtk_grid_attach(GTK_GRID(grid), child3, 1, 0, 1, 1);
    gtk_grid_insert_row(GTK_GRID(grid), 0);

    gtk_grid_attach(GTK_GRID(grid), child2, 2, 0, 1, 1);
    gtk_grid_insert_row(GTK_GRID(grid), 0);

    gtk_grid_attach(GTK_GRID(grid), child1, 3, 0, 1, 1);
//==========================================================

未加函数前的效果:
GtkGrid demo4.c

使用函数来达成跟插入列一样的界面:
GtkGrid demo4.c

上一篇:Python常用GUI框架有哪些?Python基础入门


下一篇:SQLMAP GUI (注入懒人神器)