PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 8: How to set the background color of GtkLabel?
Written by kksou   
Wednesday, 13 September 2006
Problem

You want to achieve the following:

How to set the background color of GtkLabel?


Solution
  • Create a GtkEventBox
  • Place the label in the EventBox
  • Set the background color of the eventbox with modify_bg

Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
<?php
$window = &new GtkWindow();
$window->connect_simple('destroy', array( 'Gtk', 'main_quit'));
$window->set_size_request(400,200);

// create a vbox to hold multiple labels
$vbox = new GtkVBox();

// create an eventbox that allows you set background color
$eventbox = new GtkEventBox();
$eventbox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse('#ffff00'));

// create the title label and stuff it in the eventbox
$title = new GtkLabel('This is the title');
$title->set_size_request(100,48); // note 1
$eventbox->add($title);
$vbox->pack_start($eventbox, 0, 0); // note 2

  • 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

GtkLabel is one of those "transparent" widgets. This means that it will take on the color of the container widget in which it's residing. Since it is transparent, there is no way you can set its background color.

In order to have change the color of a label, one way is to create a GtkEventBox, place the label in it, and change the background color of the eventbox with modify_bg. The net effect is you have a colored label!

In the example above, we place the title label in the eventbox and set the background color of the eventbox to yellow.

Note 1: this sets the size of the title.

Note 2: this make sure that php-gtk doesn't change the size of title.

Note 3: this makes the body text stretechable, that is, it will take on whatever space remains below the title.


Notes

Note that GtkWindow can only hold one widget at a time. If you need to stuff multiple widgets, use a container widget such as GtkVBox as in this example.


User reviews   Average user ratings:    3.0   (from 3 users)
  1. aparna
    December 20, 2007 1:34am

    thanks

  2. Tom Bell from Australia
    September 23, 2008 10:46pm

  3. Adawo
    January 11, 2009 11:55am

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2009. kksou.com. All Rights Reserved