neptuneray has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am rewriting a Tk program for Gtk2+. I am trying to set the background color of a widget (actually, its container) to white. But I can't figure out how. I have Googled all over, with no success, so I come here.

I have no trouble setting the foreground color. I'm thinking it might have to do with "color map" "channels"(?), but this is all way over my head. Here is what I've done so far:
# this will yield Medium Blue
my $red   = 0x0000;
my $green = 0x0000;
my $blue  = 0xffff;
my $fgColor = Gtk2::Gdk::Color->new($red, $green, $blue);
my $red   = 0xffff;
my $green = 0xffff;
my $bgColor = Gtk2::Gdk::Color->new($red, $green, $blue);
my $state = 'normal';

my $title = Gtk2::Label->new('The Big Show');
$title->modify_fg($state, $fgColor);  # works
$title->modify_bg($state, $bgColor);  # doesn't work 
If I set_markup with a span tag, and set background="white", it works, but looks ugly because the entire container background is not white, just the letters.

I have also tried to modify the bgColor of the HBox, and the Frame that contains it, but that doesn't seem to be supported.
Finally, I've tried playing with RC Style, but here I get really lost:
my $rcStyle = Gtk2::RcStyle->new();
my $rcColor = $rcStyle->fg($state, $fgColor);
my $rcBgColor = $rcStyle->bg($state, $bgColor);
Maybe I need to use Pango methods?? Can anyone point me in the right direction? Thanks so much,
Ray

Replies are listed 'Best First'.
Re: Setting widget bg color in Gtk2
by eosbuddy (Scribe) on Aug 10, 2008 at 02:20 UTC
    Its difficult to say why you have problems with the background color... please take a look at: http://ometer.com/gtk-colors.html (particularly at the section that says: Why doesn't setting the color work for me?). It's possible that you might need to create a container/eventbox. Another resourceful place (although for php - but it should be pretty straightforward to implement in perl) - you may need to register to see the entire code: http://www.kksou.com/php-gtk2/articles/set-the-background-color-of-GtkLabel.php Have fun:-)
      eosbuddy,

      I just found the reason explained at:
      http://www.daa.com.au/pipermail/pygtk/2007-July/013988.html

      Note:
      modify_bg() only affects widgets that have an associated gtk.gdk.Window.

      The weird thing is that calling the modify_bg method on HBox does not throw an error.

      Thanks again,
      Ray
      eosbuddy,

      Yes, packing the label into an EventBox and setting its bg_color worked! Thank you very much for your quick response.

      However, in re-writing the code, I think I might have found something that was screwing with my HBox color getting set right. If I find something, I'll post that solution also.

      Ray
Re: Setting widget bg color in Gtk2
by zentara (Cardinal) on Aug 10, 2008 at 14:04 UTC
    Tk is so much easier concerning colors, isn't it? Here is one trick that took me awhile to figure out, concerning bg colors and overriding themes. My theme Improve the look of your Perl/Gtk2 apps has many pixmap based elements. If I want to modify bg colors, you first need to remove the pixmaps, or else any bg color change will not work. Comment out the parse_string block and see the bg color changes won't take affect.

    I mention this in case you need to worry about portability. Many installations are using pixmap based themes now, so people running your code, will not be seeing what you think. In discussions on the maillists, all the experts say "do not try to override someone's theme". I guess that's the way it's going, even though I personally like to customize colors.

    I would say to be sure that your end user is seeing what you want, use the pango markup, as shown in the second example.

    #!/usr/bin/perl use warnings; use strict; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; Gtk2::Rc->parse_string(<<__); style "default" { bg_pixmap[NORMAL] = "<none>" bg_pixmap[INSENSITIVE] = "<none>" bg_pixmap[ACTIVE] = "<none>" bg_pixmap[PRELIGHT] = "<none>" bg[NORMAL] = { 1.0, 1.0, 1.0 } }class "GtkWidget" style "default" __ my $greyl = Gtk2::Gdk::Color->new (0x9999,0x9999,0x9999); my $bluel = Gtk2::Gdk::Color->new (0,0xCCCC,0xFFFF); my $red = Gtk2::Gdk::Color->new (0xFFFF,0,0); my $white = Gtk2::Gdk::Color->new (0xFFFF,0xFFFF,0xFFFF); my $window = Gtk2::Window->new('toplevel'); $window->signal_connect( destroy => sub { Gtk2->main_quit; } ); $window->set_title("Label"); my $vbox = Gtk2::VBox->new( FALSE, 5 ); $window->add($vbox); $window->set_border_width(5); $window->set_size_request(300,200); #$window->modify_bg('normal',$greyl); my $frame = Gtk2::Frame->new("Colored Label"); my $label = Gtk2::Label->new("This is a Colored Label"); $label->modify_fg('normal', $white); $frame->modify_bg('normal', $red); $vbox->pack_start( $frame, FALSE, FALSE, 0 ); #used for coloring background my $coleventb0 = Gtk2::EventBox->new(); $coleventb0->modify_bg ('normal', $bluel); $coleventb0->set_border_width(2); $frame->add($coleventb0); $coleventb0->add($label); $window->show_all; Gtk2->main;
    #! /usr/bin/perl use warnings; use strict; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; my $window = Gtk2::Window->new('toplevel'); $window->signal_connect('delete_event' => sub { Gtk2->main_quit; }); $window->set_border_width(5); $window->set_position('center_always'); my $vbox = &ret_vbox(); #add and show the vbox $window->add($vbox); $window->show(); Gtk2->main(); sub ret_vbox { #create a vbox to pack the following widgets my $vbox = Gtk2::VBox->new(FALSE,5); #create a label that will demo pango markup my $label_w_markup = Gtk2::Label->new(); $label_w_markup->set_markup("<span background = 'black' foreground= + 'green' size=\"40000\">Label with <i>markup</i></span>"); $vbox->pack_end($label_w_markup,FALSE,FALSE,4); $vbox->pack_end(Gtk2::HSeparator->new(),FALSE,FALSE,4); $vbox->show_all(); return $vbox; }

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      Zentara,

      Thanks for coming to my rescue before I got knee deep in Gtk2+ coding, and then finding I had done color schemes all the wrong way!

      I had been reading about pixmaps and Pango markup, but skipped over it in order to get something working fast.

      Now, I'll go back and do it right (the user-customizable Gnome way).

      Thanks again,
      Ray