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

In reply to Re: Setting widget bg color in Gtk2 by zentara
in thread Setting widget bg color in Gtk2 by neptuneray

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.