I'm trying to use override_background_color on some GTK3 widgets but they still showing up with the default background color.

  • Can you post the code that you used in the end? – Mendhak Jul 7 '12 at 21:24
  • I"ve dropped it because I didn't have the time. Will try it later. – int_ua Jul 8 '12 at 8:54
  • I managed to do it here by using CSS in the GTK3 widget. – Mendhak Jul 22 '12 at 7:51

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.

  • I've tried it first but in python there was no gdk.color_parse() function IIRC. Let me re-check... – int_ua Jun 29 '12 at 13:47
  • i am not familiar with pygtk but you can take a look here pygtk.org/docs/pygtk/class-gdkcolor.html – dlin Jun 29 '12 at 13:52
  • 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 – int_ua Jun 29 '12 at 13:54
  • 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 Jun 29 '12 at 13:57
  • 1
    In python you can do something like: [your widget].override_background_color(Gtk.StateFlags.NORMAL,Gdk.RGBA(1.0,0.0,0.0,1)) or [your widget].modify_bg(Gtk.StateFlags.NORMAL,Gdk.color_parse("red")) – AndAC Jun 29 '12 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.