It's very difficult to do. I've been bitching about the difficulty of setting Gtk2 widget colors for awhile, and I'm constantly slapped down by the gurus, saying " you are not supposed to interfere with (or override) the themes setup by the user.

Basically, a gtk2 app will check the theme settings in ~.gtk2rc-2.0 and holds onto them for dear life. Some you can override, but you generally need to override the theme settings explicitly, before you can change them.

The following script tries to demonstrate it. My rc parsing dosn't seem to work for Buttons(for me anyways), but will work on window widgets. Anyways, I try to override the existing theme, then make new settings,but the override dosn't work. As proof of this, rename your .gtkrc-2.0 to .gtkrc-2.0zzz and run the script, and you will see the button color change. So...... what you probably need to do, is setup a custom .gtkrc-2.0 for your app, and at the very start of your script, parse your custom rc settings. I would guess that you would have to setup a resource directory for your script, and put your custom .gtkrc-2.0 in there.

Like I said, try this after renaming your .gtkrc-2.0 and see the difference. I've included a second sample below, to show how to override a background pixmap.

#!/usr/bin/perl use warnings; use strict; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; #GTK_STATE_NORMAL State during normal operation. #GKT_STATE_ACTIVE State of a currently active widget, such as a d +epressed button. #GTK_STATE_PRELIGHT State indicating that the mouse pointer is over + the widget #GTK_STATE_SELECTED State of a selected item, such the selected row + in a list. #GTK_STATE_INSENSITIVE State indicating that the widget is unresponsiv +e to user actions. #these parsings should work, BUT they DON'T for Buttons :-( 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" __ Gtk2::Rc->parse_string(<<__); style "button" { 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 "GtkButton" style "button" __ my $window = Gtk2::Window->new('toplevel'); $window->set_title('Z'); $window ->signal_connect( 'destroy' => \&delete_event ); $window->set_border_width(10); $window->set_size_request(300,200); my $greyh = Gtk2::Gdk::Color->new (0xCCCC,0xCCCC,0xCCCC); my $greyl = Gtk2::Gdk::Color->new (0x9999,0x9999,0x9999); my $redh = Gtk2::Gdk::Color->new (0xFFFF,0,0); my $redl = Gtk2::Gdk::Color->new (0xAAAA,0,0); my $greenh = Gtk2::Gdk::Color->new (0,0xFFFF,0xEEEE); my $greenl = Gtk2::Gdk::Color->new (0,0xFFFF,0xCCCC); my $greend = Gtk2::Gdk::Color->new (0,0xFFFF,0); my $blueh = Gtk2::Gdk::Color->new (0,0xFFFF,0xFFFF); my $bluel = Gtk2::Gdk::Color->new (0,0xCCCC,0xFFFF); $window->modify_bg ('normal', $greenl); my $vbox = Gtk2::VBox->new( FALSE, 6 ); $window->add($vbox); $vbox->set_border_width(2); my $hbox= Gtk2::HBox->new( FALSE, 6 ); $vbox->pack_end($hbox,FALSE,FALSE,0); $hbox->set_border_width(2); my $frame0 = Gtk2::Frame->new('Controls'); $vbox->pack_end( $frame0, FALSE, FALSE, 0 ); $frame0->set_border_width(3); $frame0->modify_bg ('normal', $redl); #used for coloring frame my $coleventb0 = Gtk2::EventBox->new(); $frame0->add($coleventb0); my $hbox0 = Gtk2::HBox->new( FALSE, 6 ); $coleventb0->add($hbox0); $hbox0->set_border_width(3); $coleventb0-> modify_bg('normal', $greyl); my $button = Gtk2::Button->new_from_stock('gtk-quit'); $hbox0->pack_end( $button, FALSE, FALSE, 0 ); $button->signal_connect( clicked => \&delete_event ); my $button1 = Gtk2::Button->new('Color-test'); $hbox0->pack_end( $button1, FALSE, FALSE, 0 ); $button1->signal_connect( clicked => sub{ print chr(07),"\n"; # \n or use $|++ }); $button1->modify_bg ('normal', $bluel); $button1->modify_bg ('prelight', $blueh); $button1->modify_bg ('active', $greend); $coleventb0-> modify_bg('normal', $greyl); $window->show_all(); Gtk2->main; ##################################### sub delete_event { Gtk2->main_quit; return FALSE; } __END__ #!/usr/bin/perl use warnings; use strict; use Gtk2 -init; use Glib ':constants'; my $pwd = `pwd`; print "$pwd\n"; Gtk2::Rc->parse_string(<<__); pixmap_path '/home/zentara/perlplay/Gtk2/styles/background-from-base6 +4-image' style "default" { bg_pixmap[NORMAL] = "" } class "*" style "default" __ my $window = Gtk2::Window->new('toplevel'); $window->signal_connect('delete_event' => sub { exit;}); $window->set_border_width(5); $window->set_position('center_always'); $window->set_default_size(500,500); $window->show(); print Gtk2::Rc->get_theme_dir,"\n"; Gtk2->main();

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

In reply to Re: Gtk2: changing Button Color by zentara
in thread Gtk2: changing Button Color by deadpickle

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.