简体   繁体   中英

How to change Background color of child in GTK3 (c language) with CSS

Like the Title says I'm developing an App and I need to set the Background of all 4 child, but I have no clue how to do it.

Here is an Example which explains how the App looks like: 在此输入图像描述

I need to change the backgrounds like this:

  • One - should be Red
  • Two - should be yellow
  • Three - Should be green
  • Four - Should be Blue

Here is the Code:

#include <gtk/gtk.h>

int main(int argc, char *argv[]){
        //---------- CSS -------------
    GtkCssProvider *provider;
    GdkDisplay *display;
    GdkScreen *screen;
    //---------------------------

    GtkWidget *window;
    GtkWidget *child, *child2, *child3, *child4;
    GtkWidget *grid;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Equalizer");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_container_set_border_width(GTK_CONTAINER(window), 5);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    // ---------------------------------------------------- CSS -----------------------------------------------------------
    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);

    const gchar *myCssFile = "mystyle.css";
    GError *error = 0;

    gtk_css_provider_load_from_file(provider, g_file_new_for_path(myCssFile), &error);
    g_object_unref (provider);
    // --------------------------------------------------------------------------------------------------------------------

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

    child = gtk_label_new ("One");
    gtk_grid_attach (GTK_GRID (grid), child, 0, 1, 1, 1);

    child2 = gtk_label_new ("Two");
    g_object_set (child, "margin", 55, NULL);
    gtk_grid_attach_next_to (GTK_GRID (grid), child2, child, GTK_POS_RIGHT, 50, 50);


    child3 = gtk_label_new ("Three");
    g_object_set (child3, "margin", 55, NULL);
    gtk_grid_attach_next_to (GTK_GRID (grid), child3, child2, GTK_POS_RIGHT, 50, 50);

    child4 = gtk_label_new ("Four");
    g_object_set (child4, "margin", 0, NULL);
    gtk_grid_attach_next_to (GTK_GRID (grid), child4, child3, GTK_POS_RIGHT, 50, 50);


    gtk_widget_show_all(window);
    gtk_main();
}

and the CSS file:

* {
    background-color: yellow;
}

GtkWindow {
    background-color: green;
    border-width: 3px;
    border-color: blue;
}

How do I do it in GTK3 using CSS ?

You probably need to give each of your widgets a name in order to give them styles by ID, eg #child3 .

The name you would need to set in this case would be child3 .

void
gtk_widget_set_name (GtkWidget *widget,
                     const gchar *name);

https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-set-name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM