How do I change the background-color of a widget in runtime?

Gtk.StyleContext deprecated now. How can I dynamically create a CSS class with the background-color value substituted during the execution of the program?

Deprecation means “this API will be removed in the next major version”; you can keep using API deprecated in GTK4 until you need to port to GTK5.

By adding the GtkCSSProvider to the display of the widget, instead of adding it to the style context of the widget. This has always been the proper way, as style providers attached to a specific style context do not cascade.

2 Likes