Updated 2012-01-12 13:27:22 by RLE

Richard Suchenwirth 2006-01-04 - Here's a cute little code snippet that I just have to share. If you display lines in a text widget, you might want to have every second line in another background color:

Here's all it takes (computing the tag name from the line number modulo 2 - "1" is colorful, "0" is ignored):
 package require Tk

 pack [text .t]
 .t tag configure 1 -background lightblue

 set line -1
 foreach item {these are some words for testing} {
     .t insert end $item\n [expr [incr line]%2]

Lessons learned:

  • text tag names may look like integers (unlike canvas tag names, where integers are IDs)
  • Undefined tag names don't make problems