How to style GTK + with custom CSS?

can anyone introduce a simple example for styling a GTK + widget using css? I could not figure out how to do this by 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; } 
+6
source share
1 answer

I donโ€™t know anything about styling CSS in GTK3 (except that it often breaks down, because developers prefer to fix things - respect CSS standards - than maintain compatibility through versions).

However, I can tell you that this is wrong:

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

What do you really want to stop GTK when the main window is closed. This is done as follows:

 g_signal_connect(window,"destroy", G_CALLBACK(gtk_main_quit), NULL); 

One more note: you are calling

 gtk_widget_show(window); gtk_widget_show(button); 

When the widget tree is complete (i.e. you stopped adding widgets in containers and containers in the top-level window), this can be simplified:

 gtk_widget_show_all(window); 

Also note that a running program should return 0 on success and use non-zero values โ€‹โ€‹for report errors.

+1
source

All Articles