Chapter 14. Label Widgets

Inheritance Hierarchy

Object
   +--- Widget
         +--- Misc
               +--- Label
         

Labels are used a lot in GTK, and are relatively simple. Labels emit no signals as they do not have an associated X window. If you need to catch signals, or do clipping, place it inside an EventBox or a Button widget.

To create a new label, use:

$label = new Gtk::Label( $string );

The sole argument is the string you wish the label to display.

To change the label's text after creation, use the function:

$label->set_text( $string );

Once again, the argument is the new string.

The space needed for the new string will be automatically adjusted if needed. You can produce multi-line labels by putting line breaks in the label string.

To retrieve the current string, use:

$sting = $label->get();

The label text can be justified using:

$label->set_justify( $jtype );

Values for $jtype are:

'left'
'right'
'center' (default)
'fill'

The label widget is also capable of line wrapping the text automatically. This can be activated using:

$label->set_line_wrap( $wrap );

The $wrap argument takes a true or false value.

If you want your label underlined, then you can set a pattern on the label:

$label->set_pattern( $pattern );

The pattern argument indicates how the underlining should look. It consists of a string of underscore and space characters. An underscore indicates that the corresponding character in the label should be underlined. For example, the string

"_ _ _ _ _"

would underline the first, third, fifth, seventh, and ninth characters.

14.1. Label Example

Below is a short example to illustrate these functions. This example makes use of the Frame widget to better demonstrate the label styles. You can ignore this for now as the frame widget is explained later on.

Label Example Source

      
#!/usr/bin/perl -w

use Gtk;
use strict;

set_locale Gtk;
init Gtk;

my $false = 0;
my $true = 1;

my $window;
my $hbox;
my $vbox;
my $frame;
my $label;


# Create the window
$window = new Gtk::Window( "toplevel" );
$window->signal_connect( "destroy", sub { Gtk->exit( 0 ); } );
$window->set_title( "Label" );

$vbox = new Gtk::VBox( $false, 5 );
$hbox = new Gtk::HBox( $false, 5 );

$window->add( $hbox );
$hbox->pack_start( $vbox, $false, $false, 0 );
$window->border_width( 5 );

# Create labels and add them
$label = new Gtk::Label( "This is a Normal Label" );
$frame = new Gtk::Frame( "Normal Label" );
$frame->add( $label );
$vbox->pack_start( $frame, $false, $false, 0 );

$label = new Gtk::Label( "This is a Multi-line label.\nSecond line\n"
			 . "Third Line" );
$frame = new Gtk::Frame( "Multi-line Label" );
$frame->add( $label );
$vbox->pack_start( $frame, $false, $false, 0 );

$label = new Gtk::Label( "This is a Left-Justified\n"
			 . "Multi-line label.\nThird      line");
$label->set_justify( 'left' );
$frame = new Gtk::Frame( "Left Justified Label" );
$frame->add( $label );
$vbox->pack_start( $frame, $false, $false, 0 );

$label = new Gtk::Label( "This is a Right-Justified\nMulti-line label.\n"
			 . "Fourth line, (j/k)" );
$label->set_justify( 'right' );
$frame = new Gtk::Frame( "Right Justified Label" );
$frame->add( $label );
$vbox->pack_start( $frame, $false, $false, 0 );

$vbox = new Gtk::VBox( $false, 5 );
$hbox->pack_start( $vbox, $false, $false, 0 );
$frame = new Gtk::Frame( "Line wrapped label" );
$label = new Gtk::Label( "This is an example of a line-wrapped label.  It "
			 . "should not be taking up the entire             "
			 . "width allocated to it, but automatically "
			 . "wraps the words to fit.  "
			 . "The time has come, for all good men, to come to "
			 . "the aid of their party.  "
			 . "The sixth sheik's six sheep's sick.\n"
			 . "     It supports multiple paragraphs correctly, "
			 . "and  correctly   adds "
			 . "many          extra  spaces. " );
$label->set_line_wrap( $true );
$frame->add( $label );
$vbox->pack_start( $frame, $false, $false, 0 );

$frame = new Gtk::Frame( "Filled, wrapped label" );
$label = new Gtk::Label( "This is an example of a line-wrapped, filled label. "
			 . "It should be taking "
			 . "up the entire             width allocated to it.  "
			 . "Here is a sentence to prove "
			 . "my point.  Here is another sentence. "
			 . "Here comes the sun, do de do de do.\n"
			 . "    This is a new paragraph.\n"
			 . "    This is another newer, longer, better "
			 . "paragraph.  It is coming to an end, "
			 . "unfortunately." );
$label->set_justify( 'fill' );
$label->set_line_wrap( $true );
$frame->add( $label );
$vbox->pack_start( $frame, $false, $false, 0 );

$frame = new Gtk::Frame( "Underlined label" );
$label = new Gtk::Label( "This label is underlined!\n"
			 . "This one is underlined in quite a funky fashion");
$label->set_justify( 'left' );
$label->set_pattern( "_________________________ _ _________ "
		     . "_ ______     __ _______ ___");
$frame->add( $label );
$vbox->pack_start( $frame, $false, $false, 0 );

$window->show_all();

main Gtk;
exit( 0 );


# END EXAMPLE PROGRAM
      
   

Label Example Screenshot