Sample Code 23: How to set the background color of GtkButton? |
|
Written by kksou
|
|
Monday, 18 September 2006 |
|
Problem You want to set the background color of buttons as shown below:

Solution
Sample Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?php $window = new GtkWindow(); $window->connect_simple('destroy', array( 'Gtk', 'main_quit')); $window->set_size_request(400,150);
$window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Set Background Color of Button"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 60); $vbox->pack_start($title, 0, 0);
$vbox->pack_start($hbox=new GtkHBox(), 0, 0); // note 1
create_button($hbox, 'Orange', "#FFCC66"); // note 2
create_button($hbox, 'Green', '#CCFF99'); create_button($hbox, 'Blue', '#CCFFFF');
function create_button($hbox, $button_label, $bg_color) { // note 2
$button = new GtkButton($button_label); $button->set_size_request(80, 32); // note 3
$button->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse($bg_color)); // note 4
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation
- Create a GtkHBox to hold the buttons.
- Wrote a small function to simplify the creation of buttons.
- Set the size of the button.
- Set the background color of the button.
- Add the button to the hbox
- Add an event handler to respond to button click.
- This is the callback function that is called when the user clicks the button.
Notes
Setting the background color of a GtkButton is relatively easier than setting the background color of a GtkLabel.
To set the background of a GtkLabel, please refer to How to set the background color of GtkLabel?
User reviews Average user ratings: 3.5 (from 11 users) Note: You have to be a registered member to leave a comment. Free registration here. |
February 26, 2007 4:04pm
How to set the background color of GtkButton?
The code does not work on my Windows XP machine. By that I mean the button backgrounds do not display in the different colors. I am using Gnope release 1.5.1.
February 26, 2007 6:09pm
That's strange. I just tried it on both winxp and linux. It's working fine. Have you tried the example on "How to set the background color of GtkLabel"? Is that one working?
Would be great if anyone trying out this example let us know if you are facing the problem too.
February 27, 2007 7:38am
The example for changing the background color of GtkLabel works on my machine.
February 27, 2007 7:56am
Let's be a bit more specific so that I know what to fix.
1. When you first run the above example, do you see the three buttons in color?
2. When you move your mouse over the button, they turn back to gray.
Is this what you encountered?
February 27, 2007 8:19am
No. When I first run the example all 3 buttons have gray backgrounds. When I move the mouse over the buttons the backgrounds remain gray but the border highlights change to orange.
February 27, 2007 10:06am
Strange. I tried on two other machines and the example runs ok.
Any other people have the same problem?
Alternatively, would be great if you could
1. download the latest Gnope and try again. I suspect maybe your gtk2.dll does not have the constant Gtk::STATE_NORMAL defined, or
2. try it on another machine and see if it's the same.
March 04, 2007 1:32pm
Quite by accident I found a way to make the button backgrounds display. If I insert a string(of any size) on its own line in the gtkrc file, everything displays properly.
April 12, 2007 8:14pm
What a weird behaviour...I'm using WinXP and the only way I had the background color to be applied correctly was renaming the gtkrc file. Does anybody know about that?
July 16, 2008 5:30pm
This definitely does NOT work, using the current
php-gtk download. Tried messing with our gtkrc file, w/o success. Maybe it works with Gnope, but we can't use Gnope, for various reasons. Still researching what it will take to get our button backgrounds set.
July 16, 2008 6:47pm
Hi M Drake,
It does work.
Assuming you're using the latest v2.0.1, go to <php-gtk root folder>/etc/gtk-2.0 and open the file gtkrc in your favorite editor.
Comment out lines 45, 46 and 47 as follows:
#engine "wimp"
#{
#}
Save and close the file.
Now try running the sample code again. Did it work this time?
Regards,
/kksou
July 17, 2008 11:37am
That fixed it, thanks. Also found the cause of my problem with images on the buttons.
Did somebody really think this was a good idea?