Chapter 22. Entry Widget

Inheritance Hierarchy

Object
   +--- Widget
         +--- Editable
               +--- Entry
         

The Entry widget allows text to be typed and displayed in a single line text box. The text may be set with function calls that allow new text to replace, prepend or append the current contents of the Entry widget.

There are two functions for creating Entry widgets:

$entry = new Gtk::Entry();

$entry = new Gtk::Entry( $max_length );

The first just creates a new Entry widget, while the second creates a new Entry and sets a limit on the length of the text within the Entry.

There are several functions for altering the text which is currently within the Entry widget.

$entry->set_text( $text );

$entry->append_text( $text );

$entry->prepend_text( $text );

The function set_text() sets the contents of the entry widget, replacing the current contents. The functions append_text() and prepend_text() allow the current contents to be appended and prepended to.

If we are using the Entry where we don't want the text entered to be visible, for example when a password is being entered, we can use the following function, which also takes a true or false value:

$entry->set_visibility( $visible );

If we want to catch when the user has entered text, we can connect to the 'activate' or 'changed' signal. Activate is raised when the user hits the enter key within the Entry widget. Changed is raised when the text changes at all, e.g., for every character entered or removed.

22.1. Text Entry Example

The following code is an example of using an Entry widget.

Text Entry Source

      
#!/usr/bin/perl -w

use Gtk;
use strict;

set_locale Gtk;
init Gtk;

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

my $window;
my $vbox;
my $hbox;
my $entry;
my $button;
my $check;


# Create a window
$window = new Gtk::Window( "toplevel" );
$window->set_usize( 200, 100 );
$window->set_title( "GTK Entry" );
$window->signal_connect( "delete_event", sub { Gtk->exit( 0 ); } );

$vbox = new Gtk::VBox( $false, 0 );
$window->add( $vbox );
$vbox->show();

# Create the Entry
$entry = new Gtk::Entry( 50 );
$entry->signal_connect( "activate", \&enter_callback, $entry );
$entry->set_text( "Hello" );
$entry->append_text( "World" );
$entry->select_region( 0, length( $entry->get_text() ) );
$vbox->pack_start( $entry, $true, $true, 0 );
$entry->show();

$hbox = new Gtk::HBox( $false, 0 );
$vbox->add( $hbox );
$hbox->show();

# Create the checkbox that determines if the entry is editable
$check = new Gtk::CheckButton( "Editable" );
$hbox->pack_start( $check, $true, $true, 0 );
$check->signal_connect( "toggled", \&entry_toggle_editable, $entry );
$check->set_active( $true );
$check->show();

# Create the checkbox that determines if the entry text is visible
$check = new Gtk::CheckButton( "Visible" );
$hbox->pack_start( $check, $true, $true, 0 );
$check->signal_connect( "toggled", \&entry_toggle_visibility, $entry );
$check->set_active( $true );
$check->show();

# Create the close button
$button = new Gtk::Button( "Close" );
$button->signal_connect( "clicked", sub { Gtk->exit( 0 ); } );
$vbox->pack_start( $button, $true, $true, 0 );
$button->can_default( $true );
$button->grab_default();
$button->show();

$window->show();

main Gtk;
exit( 0 );



### Subroutines


# Callback that is called when the enter key is pressed and the Entry
# widget has the focus.  It prints out the text in the entry widget.

sub enter_callback
{
   my ( $widget, $entry ) = @_;

   my $entry_text = $entry->get_text();
   print( "Entry contents: $entry_text\n" );
}


# Callback to toggle the editable property of the entry widget.

sub entry_toggle_editable
{
   my ( $checkbutton, $entry ) = @_;

   $entry->set_editable( $checkbutton->active );
}


# Callback to toggle the visible property of the entry widget.

sub entry_toggle_visibility
{
   my ( $checkbutton, $entry ) = @_;

   $entry->set_visibility( $checkbutton->active );
}


# END EXAMPLE PROGRAM
      
   

Entry Example Screenshot