003. How to set the background color of GtkWindow?

Problem

You want to set the background color of GtkWindow as follows:

How to set the background color of GtkWindow?


Solution

Use GtkWidget::modify_bg()

$window->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse("#FFFF00"));

Sample Code

1   
2   
3   
7   
8   
9   
10   
11   
12   
13   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 100);
$window->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse("#FFFF00"));
$window->connect_simple('destroy', array('Gtk','main_quit'));
$label = new GtkLabel("hello world!");
$window->add($label);
$window->show_all();
Gtk::main();
?>

Output

As shown above.
 

Explanation

The #FFFF00 is the standard hexadecimal color notation, i.e. #RRGGBB.

As for the first argument, they do have quite a bit of explanation in the php manual. But if all you want is to set the background color of the window, just use Gtk::STATE_NORMAL.

Notes

This works with some of the other widgets, but not all.

Some widgets in PHP-GTK2 are "transaparent", meaning they take on the background color of the widgets they are residing in. So using modify_bg does not work. And these transparent widgets happen to be some of the most commonly used ones, including labels and buttons.

Setting the background color of widgets such as labels and buttons requires other techniques. This is covered in "How to set the background color of GtkLabel"

Add comment


Security code
Refresh