in reply to Gtk2: changing Button Color

Hi, I asked on the Gtk2/Perl maillist and was given the tip, to set the %ENV variable to point to a custom gtkrc file. This will override the colors set as default in ~.gtkrc-2.0. Of course it requires you wrap your script in a wrapper, and you setup a (possibly empty) theme for your script, and put it in a resource directory.
#!/bin/sh #wrapper to make custom theme for a Gtk2 app export GTK2_RC_FILES='/usr/local/share/themes/Bumblebee/gtk-2.0/gtkrc' ./button-color3-env-works # run the script

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

Replies are listed 'Best First'.
Re^2: Gtk2: changing Button Color
by zentara (Cardinal) on Aug 24, 2007 at 16:41 UTC
    To update this, for anyone searching, you don't need a wrapper for a Perl script (although it would be useful for a c program).

    Anyways, you can use a BEGIN block, or set the ENV and require Gtk2 instead of use to load it.

    What this script does, is load an empty gtkrc file (in the cwd), and it overrides the theme in the /home/user/.gtkrc-2.0 file. Thus making button color changes possible.

    #!/usr/bin/perl use warnings; use strict; use Glib qw/TRUE FALSE/; # must be before Gtk2 init #BEGIN { $ENV{GTK2_RC_FILES} = 'gtkrc_myapp' } #use Gtk2 '-init'; # or set and require module below $ENV{GTK2_RC_FILES} = 'gtkrc_myapp'; #require Foo; #Foo->import; # if necessary require Gtk2; Gtk2->init; 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; }

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