my $text = $mw->Text( [ option => value ] );Some basic options are
#!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.
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->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:
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