8.6. 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 list of tag names to assign to the inserted text. The usage is:
$text->insert(index, string, [ taglist, string, taglist ...] )
So far we've only seen single tags used with insert. If you want to specify more than one tag, put the tag names into square brackets, creating a list:
$t->insert('end', "This is a very tagged line",
[ 'tag1', 'tag2', 'tag3' ]);
To use different sets of tags, you can 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, etc. 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.