How to change GtkWidget background in GTK3?
12,596
GTK+ 1.2
GtkRcStyle *rc_style;
GdkColor color;
color.red = 65535;
color.green = 0;
color.blue = 0;
rc_style = gtk_rc_style_new();
rc_style->bg[GTK_STATE_NORMAL] = color;
rc_style->color_flags[GTK_STATE_NORMAL] |= GTK_RC_BG;
gtk_widget_modify_style (widget, rc_style);
gtk_rc_style_unref (rc_style);
GTK+ 2.24
Using the GDK Library:
GtkWidget *widget; //your widget
GdkColor color;
gdk_color_parse ("red", &color); //setting a color - you can also use RGB
gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, &color); //modifying the background color of the widget
Full reference about GdkColor can be found at GNOME'S Dev Documentation.
GTK 3.0
For gtk+3 you can use gtk_css_provider() .Full tutorials and examples can be found at the GTK+ Forums.
Related videos on Youtube
Author by
int_ua
I dream of a day when settings are written to disk only when changed and software does not freak out when $HOME is read-only.
Updated on September 18, 2022Comments
-
int_ua over 1 year
I'm trying to use override_background_color on some GTK3 widgets but they still showing up with the default background color.
-
Mendhak almost 12 yearsCan you post the code that you used in the end?
-
int_ua almost 12 yearsI"ve dropped it because I didn't have the time. Will try it later.
-
-
int_ua almost 12 yearsI've tried it first but in python there was no
gdk.color_parse()
function IIRC. Let me re-check... -
dlin almost 12 yearsi am not familiar with pygtk but you can take a look here pygtk.org/docs/pygtk/class-gdkcolor.html
-
int_ua almost 12 yearsbesides:
Warning gtk_widget_modify_bg has been deprecated since version 3.0 and should not be used in newly-written code. Use gtk_widget_override_background_color() instead
-
dlin almost 12 yearsim still using GTK+ 2.24.Can you edit your post with the code which you use to change the background color?that would help
-
dlin almost 12 yearsadditionally, what is the widget you are trying to modify?
-
dlin almost 12 yearsI edited my answer with more information.
-
int_ua almost 12 yearsadding the code...
-
erin c almost 3 yearsgtk 3 forum link is broken