Text

In some cases the one-line limitation of the text entry widget is inconvenient for data entry. A common example of this is the provision of an area to enter some comments. The text widget is used when there are multiple lines of input being accepted. We will discuss this widget in more detail in a later chapter, but include a summary here for completeness. The basic syntax is
my $text = $mw->Text( [ option => value ] );
Some basic options are As well, there are some common methods An simple example of its use is
#!perl
# file text.pl
use Tk;
use strict;
use warnings;
my $text_value = 'blank';
my $message = "The comment you entered is $text_value";
my $mw = MainWindow->new;
$mw->title('text');
my $label = $mw->Label(-textvariable => \$message);
my $enter = $mw->Label(-text => 'Enter a comment');
my $text = $mw->Text(-width => 40, -height => 16);
$text->insert('end', 'Please enter a comment here');
my $show = $mw->Button(-text => 'Show comment',
                       -command => \&display);
my $exit = $mw->Button(-text => 'Exit',
                       -command => [$mw => 'destroy']);
$label->pack;
$enter->pack;
$text->pack;
$show->pack;
$exit->pack;
MainLoop;
sub display {
  my $comment = $text->get('1.0', 'end');
  $message = "The comment you entered was: \n $comment";
  $text->delete('1.0', 'end');
  $text->insert('end', $comment);
}
which appears below.

Figure 3.15: Example of a text widget
Image text

Like for the first example of the entry widget, here the $show button, when clicked, will call the display subroutine. This routine will fetch the current value of the $text text widget, update the $message string with that value, and then clear the entered text with the delete method. The insert method will subsequently put into the text area the comment just entered. The $message string is subsequently used as the text for the $label label.

Formatting and binding of portions of the text can be done with tags. Tags can be created with the tagConfigure method:

  $text->tagConfigure(name, [options])}
where name is the name of the tag and options are used to configure the tagged text; some examples are Text can then formatted be formatted according to the tag options by, for example, giving the tagname as an option to the insert method:
  $text->insert('end', ''string'', tagname);
Bindings on tags can be specified through the tagBind method:
  $text->tagBind(tagname, sequence, callback);
Here is a short example of creating some text which acts like a hyperlink: when the cursor enters the region of the tagged text, the color of the line changes, and the cursor changes into a hand.
#!perl
# file text1.pl
use Tk;
use strict;
use warnings;
my $mw = MainWindow->new;
$mw->title('text1');
my (@bold, @normal);
if ($mw->depth > 1) {
  @bold   = (-background => '#43ce80',  qw/-relief raised -borderwidth 1/);
  @normal = (-background => undef, qw/-relief flat/);
}
else {
  @bold   = (qw/-foreground white -background black/);
  @normal = (-foreground => undef, -background => undef);
}
my $text = $mw->Text(-width => 40, -height => 16);
$text->tagBind('link', '<Any-Enter>' => 
	       sub {my $l = shift; $l->tagConfigure('link', @bold);
		$l->configure(-cursor => 'hand2');}
	      );
$text->tagBind('link', '<Any-Leave>' =>
	       sub {my $l = shift; $l->tagConfigure('link', @normal);
		$l->configure(-cursor => 'arrow');}
	      );
$text->tagBind('link', 
	       '<Button-1>' => sub{print "Hello\n";});

$text->insert('end', "Here is some text\n");
$text->insert('end', "Here is a link\n", 'link');
my $exit = $mw->Button(-text => 'Exit',
		       -command => [$mw => 'destroy']);
$text->pack;
$exit->pack;
MainLoop;
a screenshot of which appears below:

Figure 3.16: Example of a text widget with tags
Image text1

In this example 3 bindings are used: one to change the color and cursor when the mouse enters the region of the tagged text, another to set the color and cursor back to normal when the mouse leaves the region, and a third to associate a callback to when the first mouse button is clicked in the region of the tagged text.

Randy Kobes 2003-11-17