Today I start programming in GTK3, and I have a problem. I can't change the button background color, I want to change it after a click on the button. Here is my code:
void pushButton( GtkWidget* button )
{
gtk_button_set_label( GTK_BUTTON( button ), "new_text" );
GdkRGBA color = {.0, .0, .0, 1.0};
gtk_widget_override_background_color ( button, GTK_STATE_NORMAL, &color );
}
gtk_button_set_label() is working well and changes the button label. But the color is still the same (should be black).
gtk_widget_override_background_color has been deprecated since version 3.16 and should not be used in newly-written code. If you wish to change the way a widget renders its background you should use a custom CSS style.
You need to add custom CSS to the Button:
GdkDisplay *display;
GdkScreen *screen;
display = gdk_display_get_default ();
screen = gdk_display_get_default_screen (display);
GtkCssProvider *provider;
provider = gtk_css_provider_new ();
gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gtk_css_provider_load_from_data(provider, CSS, -1, NULL);
g_object_unref (provider);
Where you replace CSS with some CSS to modify the Button