home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeMastering Perl/TkSearch this book

8.4. Text Indexes

When we talked about Listbox index values, each index referred to a line in the Listbox. The first line in the Listbox was at index 0, and so on. With a Text widget, the index can point to a specific line, but it can also point to a character within that line. An index for a Text widget is built using a base index and then optionally modifying that index. The entire index, base, and modifier should be put in double quotes.

8.4.3. Text Index Examples

Here are some text index examples:

The position right after the last line of text in the widget, no matter how much text is in the widget.

The first character on the first line in the Text widget. The 1 represents the line, and 0 represents the character.

'2.0 - 1 chars'
The last character on the end of the first line. We reference it by using the first character on the second line (2.0) and subtracting one character value from that. If we use the insert method with this item, we insert the text right before the "\n" at the end of the first line.

Also the last character on the end of the first line. This is a simpler way of getting to it.

'2.0 lineend'
The end of the second line. It is necessary to specify 2.0, not just 2, because 2 is an invalid base index.

The beginning of the current selection. This index might not exist if there isn't currently a selection.

The basic indexes are easy. When you start doing index arithmetic, it becomes a little more complicated. Just remember that you are referring to a position in the Text widget that may change if other text is inserted or deleted (either by the user or the application).

Although some of the combinations may seem silly (e.g., '1.0 linestart'), keep in mind that you will most likely be calling methods that return indeterminate information about an event. For example, a user clicks in the Text widget and presses a Button that will increase the font size of that entire line. The index arithmetic allows you to reference that entire line without even knowing for sure which line it is on.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.