Inserting Text
Now that we’ve gone over text indexes and marks, we can talk in more detail about the methods for manipulating the widget’s contents.
As we’ve seen from the many examples
in this chapter, we use insert
to put text into
the Text widget. The first argument is an index and indicates where
the text will be inserted. The second argument is the string to
insert. The next argument (which is optional) is a single tag name or
a reference to an array of tag names to assign to the inserted text.
The usage is:
$text->insert(index, string, [ taglist, string, taglist ...]
)
So far we’ve seen only single tags
used with insert
. If you want to specify more than
one tag, put the tag names into square brackets, creating an
anonymous array:
$t->insert('end', "This is a very tagged line", [ 'tag1', 'tag2', 'tag3' ]);
To use different sets of tags, supply additional text lines and additional tag lists:
$t->insert('end', "This is the heading", ['heading', 'underline'], "Second line", ['bold', 'blue']);
When you use the
insert
command to insert more than one set of text
with different tags, make sure they always come in pairs: text, tags,
text, tags, and so on. If the tag used isn’t defined (with
tagConfigure
), there will be no effect on the
text, but the tag will still be assigned to that text. You can create
the tag later if you wish.
You can also insert an entire newline-delimited file with a single
insert
call:
{ local $/ = undef; $text->insert('1.0', <FILE_HANDLE>); }
We set the ...
Get Mastering Perl/Tk now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.