Chris Bao
Chris Bao

Reputation: 2868

Tcl/Tk: highlight some line in text widget or change the color for specific line text

I want to realize one effect for my Tcl/Tk tool: in the text widget, depending on the specific condition, I hope to highlight some lines' background color, other lines are normal and transparent. It is possible?
I have tried some options like: -highlightbackground ,-insertbackground and so on, but no one can do this.
if this is impossible, how to change the color of specif line text , it is also a workaround.

Upvotes: 3

Views: 5138

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137687

I hope to highlight some lines' background color, other lines are normal and transparent. It is possible?

Yes. You do it by setting a tag on the text concerned. You can then configure the text with that tag to look how you want it to, which can include changing the font, the foreground colour and the background colour. Tags are set either when you insert the text, or using the tag add method. Tags are configured with the tag configure method.

# Make a text widget and put some text in it
pack [text .t  -height 10  -width 40]
.t insert  1.0  "This is an example of tagging."

# Set some tags; they have no style as yet
.t tag add  foo  1.5 1.10
.t tag add  bar  1.15 1.20

# Configure the tags so that we can see them
.t tag configure  foo  -font {Times 16 {bold italic}}
.t tag configure  bar  -foreground yellow  -background blue

Note that the selection is actually a special tag, sel. You can configure it however you want, but the text widget's class bindings control where it is applied to in response to user actions.

Upvotes: 4

Jerry
Jerry

Reputation: 71568

You can use the Tk widget demo to help you, and more specifically the Search tool. Here I took the most essential parts of it with some edits to simplify it:

package require Tk

# proc to highlight
proc textSearch {w string tag} {
  # Remove all tags
  $w tag remove search 0.0 end
  # If string empty, do nothing
  if {$string == ""} {return}
  # Current position of 'cursor' at first line, before any character
  set cur 1.0
  # Search through the file, for each matching word, apply the tag 'search'
  while 1 {
    set cur [$w search -count length $string $cur end]
    if {$cur eq ""} {break}
    $w tag add $tag $cur "$cur + $length char"
    set cur [$w index "$cur + $length char"]
  }
  # For all the tagged text, apply the below settings
  .text tag configure search -background blue -foreground white
}


# Window set ups    
text .text -yscrollcommand ".scroll set" -setgrid true
scrollbar .scroll -command ".text yview"

frame .string
label .string.label -text "Search string:" -width 13 -anchor w
entry .string.entry -width 40 -textvariable searchString
button .string.button -text "Highlight" \
    -command "textSearch .text \$searchString search"

pack .string.label .string.entry -side left
pack .string.button -side left -pady 5 -padx 10
bind .string.entry <Return> "textSearch .text \$searchString search"

pack .string -side top -fill x
pack .scroll -side right -fill y
pack .text -expand yes -fill both

.text insert 1.0 \
{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce aliquet, neque at sagittis vulputate, felis orci posuere sapien, a tempus purus diam id tellus. Quisque volutpat pretium iaculis. Mauris nibh ex, volutpat id ligula sit amet, ullamcorper lobortis orci. Aliquam et erat ac velit auctor bibendum. Aliquam erat volutpat. Maecenas fermentum diam sed convallis fermentum. Maecenas ultricies nisi mauris, ac lacinia lacus sollicitudin eget. Mauris eget euismod nisi, sed suscipit est.}

Upvotes: 3

Related Questions