Create a text widget with the Text method:
The standard configuration options that apply to$parentwidget->Text ( options)
Text are:
-background,
-bg,
-borderwidth,
-bd,
-cursor,
-exportselection,
-font,
-foreground,
-fg,
-height,
-highlightbackground,
-highlightcolor,
-highlightthickness,
-insertbackground,
-insertborderwidth,
-insertofftime,
-insertontime,
-insertwidth,
-padx,
-pady,
-relief,
-selectbackground,
-selectborderwidth,
-selectforeground,
-state,
-takefocus,
-width,
-xscrollcommand,
and
-yscrollcommand.Other options are:
-setgrid => boolean-spacing1 => amountDefines the amount of space left on the top of a line of text that starts on its own line. Default is 0.
-spacing2 => amountDefines the amount of space left on the top of a line of text after it has been automatically wrapped by the text widget. Default is 0.
-spacing3 => amountDefines the amount of space left after a line of text
that has been ended by "\n". Default is 0.
-tabs => listSpecifies a list of tab stops to use in the text widget. Default is undefined (no tab stops).
-wrap => modeSets the mode for determining automatic line wrapping. Values
are "none" (no wrapping), "char" (wrap at any character), or "word"
(wrap at a word boundary). Default is "char".
In a Text widget, several indexes are defined to identify positions in the text widget, for use by the methods used for retrieving or manipulating text. These indexes are:
n.m@x,yThe character closest to the x,y coordinate.
endinsertcurrentmarkOther marks defined for the widget (see discussion of text marks later in this section).
sel.firstsel.lastThe character just after the last selected character.
tag.firstThe first character in the widget of the specified tag type.
tag.lastThe character just after the last character of the specified tag type.
widgetThe location of an embedded widget.
There are also several modifiers to use with text indexes. They are:
+ n lines- n linesn lines before or after the index.
+ n chars- n charsn characters before or after the index.
linestartlineendThe last character on the line (often a newline).
wordstartwordendThe character after the last character in the word.
In addition to configure and cget, the following methods
are defined for the Text widget.
bboxReturns the location and dimensions of the bounding box surrounding the character at the specified index. The returned list contains four numbers representing (respectively) the x coordinate of the upper-left corner, the y coordinate of the upper-left corner, the width of the text in pixels, and the height of the text in pixels.
comparePerforms a comparison on two indexes. For example:
if ($text->compare('insert', '==', 'end') {
# we're at the end of the text
}
The valid operators are <, <=, ==, >=, and !=.debugdeleteDeletes text from the text widget. To delete everything:
$text->delete(0, 'end');
dlineinfoReturns the location and dimensions of the bounding box surrounding the line at the specified index. The returned list contains five numbers representing (respectively) the x coordinate of the upper-left corner, the y coordinate of the upper-left corner, the width of the text in pixels, the height of the text in pixels, and the baseline position of the line.
getindexGiven a named index, returns its numeric equivalent in the
format line.char.
insertInserts text into the widget at the specified location. The second argument is the text to insert and the third argument is either a single tag or a list reference containing the names of multiple tags to apply to the text. Subsequent arguments alternate between text and tags. For example:
$text->insert('end', 'You want to do ', 'normal',
'what?!', ['bold','red']);
searchReturns the index containing a particular string in the text widget. For example, to search backwards for a case-insensitive hostname starting from the end of the text:
The$hostindex = $text->search(-nocase, -backwards, $hostname, 'end');
search method takes several switches to modify the search,
each starting with "-".
The first argument that does not start with "-" is taken to be
the search string.
The switches are:
-forwardsSearch forwards starting at the specified index.
-backwardsSearch backwards starting at the specified index.
-exactMatch the string exactly (default).
-regexpTreat the pattern as a regular expression.
-nocaseIgnore case.
-count => \$variableStore the number of matches into the specified variable.
--Interpret the next argument as the pattern. (Useful when the pattern starts with a "-".)
seeScrolls the text so that the portion of the text containing the specified index is visible.
windowEmbeds widgets within the Text widget. The first argument
can be any of: 'create', 'names', 'cget', and 'configure'.
createInserts an embedded widget at a specified index. Each widget occupies one character in the text widget. The widget must have already been created as a child of the text widget. For example:
$button = $text->Label(-text => "How ya doing?");
$text->window('create','end', -window => $button);
Here, the -window option is used to identify the widget to
embed.
The list of options to window ('create') is:
-alignDetermines the positioning within the line of text. Values are 'baseline', 'bottom', 'top', or 'center' (default).
-padxAdds padding in the x direction.
-padyAdds padding in the y direction.
-windowIdentifies the widget to embed.
namesReturns a list of widget types embedded into the text widget.
cgetReturns information on the widget at the specified index.
$text->window('cget',0);
configureConfigures widget at the specified index.
$text->window('configure',0,-background => "green");
xviewManipulates the text in view. With no arguments, returns a
list of two numbers between 0 and 1, defining what portion of the
text is currently hidden on the left and right sides,
respectively. With arguments, the function of xview
changes:
indexIf the first argument is an index, that position becomes the leftmost position in view.
movetoMoves the specified fraction of the text to the left of the visible portion.
scrollScrolls the text left or right by the specified number of units (characters, in this context) or pages. Used primarily as a callback to a scrollbar; pressing on an arrow would move by units (characters), and pressing on the trough would move by pages. The number is either 1 or -1, to move forwards or backwards, respectively.
yviewManipulates the text in view. With no arguments, returns a
list of two numbers between 0 and 1, defining what portion of the
text is currently hidden on the top and bottom,
respectively. With arguments, the function of yview
changes:
indexIf the first argument is an index, that position becomes the topmost position in view.
movetoMoves the specified fraction of the text to the top of the visible portion.
scrollScrolls the text up or down by the specified number of units (lines, in this context) or pages. Used primarily as a callback to a scrollbar; pressing on an arrow would move by units (lines), and pressing on the trough would move by pages. The number is either 1 or -1, to move forwards or backwards, respectively.
You can associate a distinct set of format properties to a
portion of the text using tags.
A tag is defined with the tagConfigure
method, and text is associated with a tag via an option to
the insert or tagAdd method. For example:
$text->Text->pack;
$text->tagConfigure('bold', -font =>
'-*-Courier-Medium-B-Normal-*-120-*-*-*-*-*-*");
$text->insert('end', "Normal text\n");
$text->insert('end', "Bold text\n", 'bold');
There are several methods defined for manipulating text tags.
They are:tagAddAdds a tag to the text within the specified index range. For example, to assign the "bold" tag defined above to the current selection:
$text->tagAdd('bold','sel.first','sel.last');
You can supply multiple ranges as arguments to tagAdd.tagBindExecutes a callback when a specified event happens on the tagged text. For example:
$text->tagBind('goto_end', "<Button-1>", sub {shift->see('end');} );
tagConfigureCreates or changes settings for a text tag, for example:
$text->tagConfigure('link', -foreground => 'red');
Options for configuring text tags are:
-background => colorThe color to use behind the text.
-bgstipple => bitmapA pattern to draw behind the text.
-borderwidth => amountThe width of the edge drawn around the text.
-fgstipple => bitmapA pattern used to draw the text.
-font => fontnameThe font used for the text.
-foreground => colorThe color of the text.
-justify => positionThe justification of the text (any of 'left', 'right', and 'center'). The default is 'left'.
-lmargin1 => amountThe indentation for the first line of a paragraph.
-lmargin2 => amountThe indentation for subsequent lines of a paragraph (for hanging indents).
-offset => amountThe amount the text is raised or lowered from the baseline (for subscripts and superscripts).
-overstrike => booleanDraws the text with a line through it.
-relief => typeThe type of edges drawn around the text. Values for
type can
be 'flat', 'groove', 'raised', 'ridge', and 'sunken'. Default is 'flat'.
-rmargin => amountThe right margin.
-spacing1 => amountThe amount of space left on the top of a line of text that starts on its own line.
-spacing2 => amountThe amount of space left on the top of a line of text after it has been automatically wrapped by the text widget.
-spacing3 => amountThe amount of space left after a line of text that has been ended by "\n".
-tabs => listA list of tab stops to use in the text widget.
-underline => booleanWhether to underline the text.
-wrap => modeSets the mode for determining automatic line wrapping. Values
are "none" (no wrapping), "char" (wrap at any character), or "word"
(wrap at a word boundary).
tagCgettagDeletetagRemoveRemoves the tags from the text in the specified index range. (The tag itself remains defined, but is no longer applied to that text.)
tagRaiseIncreases priority for a specified tag. With only one argument, the tag takes highest priority over all others; with a second argument of another tag, the tag's priority is just higher than the second tag. For example:
$text->tagRaise('bold','italic');
tagLowerDecreases priority for a specified tag. With only one argument, the tag takes lowest priority to all others; with a second argument of another tag, the tag's priority is just lower than the second tag. For example:
$text->tagLower('italic','bold');
(The outcome of this code is effectively identical to that of the previous example.)tagNamesReturns the names of all tags applying to the specified index. With no index specified, returns all tags defined for the text widget, regardless of whether they have been applied.
@defined_tags = $text->tagNames;
tagRangesReturns a list of index ranges to which the specified tag is defined. The returned list contains pairs of starting and ending indexes for the ranges.
@bold_indexes = $text->tagRanges('bold');
tagNextrangeGiven a tag name and an index, returns the next index range to which the specified tag is defined.
@next_bold = $text->tagRanges('bold', 'insert');
A mark refers to a particular position in between characters in
a text widget.
Once you create a mark, you can use it as an index. The gravity of the mark
affects which side the text is inserted. "Right" gravity is the
default, in which case the text is inserted to the right of the mark.
The two marks that are automatically set are "insert" and "current".
The "insert" mark refers to the position
of the insert cursor. The "current" mark is the position closest to
the mouse cursor.
The following methods are defined for marking:
markGravitySets the gravity of a mark. For example:
$text->markGravity('insert', 'left');
markNamesmarkSetCreates a mark at a specified index.
$text->markSet('saved', 'insert');
markUnsetDeletes mark(s) from the text widget.
$text->markUnset('saved');
Note that you cannot delete the "insert" or "current" marks.