Looking inside the Text Widget

The text widget has several operations that let you examine its contents. The simplest is get, which returns a range of text from the widget. You can get all the text with this command:

$t get 1.0 end

Looking at Tags

The tag names command returns all the tag names, or the names of the tags at a specified index:

$t tag names ?index?

A text tag can be applied to many different ranges of text. The tag ranges operation returns a list of indices that alternate between the start and end of tag ranges. The foreach command with two loop variables makes it easy to iterate through all the ranges:

foreach {start end}[$t tag ranges $tag] {
   # start is the beginning of a range
   # end is the end of a range
}

The tag nextrange ...

Get Practical Programming in Tcl & Tk, Third Edition 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.