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.

Share:
12,596

Related videos on Youtube

int_ua
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, 2022

Comments

  • int_ua
    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
      Mendhak almost 12 years
      Can you post the code that you used in the end?
    • int_ua
      int_ua almost 12 years
      I"ve dropped it because I didn't have the time. Will try it later.
  • int_ua
    int_ua almost 12 years
    I've tried it first but in python there was no gdk.color_parse() function IIRC. Let me re-check...
  • dlin
    dlin almost 12 years
    i am not familiar with pygtk but you can take a look here pygtk.org/docs/pygtk/class-gdkcolor.html
  • int_ua
    int_ua almost 12 years
    besides: 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
    dlin almost 12 years
    im 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
    dlin almost 12 years
    additionally, what is the widget you are trying to modify?
  • dlin
    dlin almost 12 years
    I edited my answer with more information.
  • int_ua
    int_ua almost 12 years
    adding the code...
  • erin c
    erin c almost 3 years
    gtk 3 forum link is broken