9

can someone type up a simple example for styling a GTK+ widget with css? I could not figure out how to do it looking at the docs:

#include <gtk/gtk.h>
int main(int argc,char *argv[])
{
    gtk_init(&argc,&argv);
    GtkWidget *window;
    GtkWidget *button;
    GtkCssProvider *cssProvider;

    gtk_css_provider_load_from_path(cssProvider,"./gtkExample2.css",NULL);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new_with_label("GTK Button");

    gtk_style_context_add_provider(gtk_widget_get_style_context(window),cssProvider,GTK_STYLE_PROVIDER_PRIORITY_USER);

    g_signal_connect_swapped(window,"delete-event",G_CALLBACK(gtk_widget_destroy),window);

    gtk_container_set_border_width(GTK_CONTAINER(window),20);
    gtk_container_add(GTK_CONTAINER(window),button);

    gtk_widget_show(window);
    gtk_widget_show(button);

    gtk_main();
    return 1;
}
8
  • I've been making attempts, I'll add what I have currently Commented Jan 19, 2013 at 3:35
  • What problem(s) are you having with that code? Commented Jan 19, 2013 at 3:38
  • Compiling wise, argument 2 passed into gtk_style_context_add_provider is not correct. It requires GtkStyleProvider and i provided GtkCssProvider and I have no idea how to get a GtkStyleProvider Commented Jan 19, 2013 at 3:47
  • 4
    I have the exact same question, and have no idea why this question was closed :/. I'd still very much like it to be answered (and could have used the answer if it had not been closed). Commented Mar 29, 2015 at 1:25
  • 1
    You're passing an uninitialized pointer (cssProvider) to gtk_css_provider_load_from_path. You should create a cssProvider first: cssProvider = gtk_css_provider_new(); Commented Nov 14, 2015 at 5:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.