Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Defining position in the text widget 1

Status
Not open for further replies.

infiniti35

Technical User
Dec 1, 2004
3
US
Hi all,
I have a script that takes the string input and displays on the text widget. What I want to do is to set up a tag when the string input contains a special word such as "ERR". I want to highlight the entire line which contains this special word. How do I find the position index of this line in the text widget and set up tag.

Thanks everyone!!

Here is a sample code:

proc append_text {widget text}
{
if [ regexp "ERR" $text ] {
$widget insert end $text
....
set up tag for this line and highlight it
....
}
else {
$widget isnert end $text
}
}
 
Since you're going to insert the text regardless, why not move that out of the If block:
Code:
proc append_text {widget text} {
  $widget insert end $text
#I think the "string" function is better/faster here than regexp
  if {[string first ERR $text] != -1} {
     #where is cursor?
    set indexin [$widget index insert]
    set i [string first "." $indexin]
    set co [string range $indexin [expr $i+1] end];#col
    set li [string range $indexin 0 [expr $i-1]];#line number
     #tag the line
    $widget tag add errTag $li.0 $li.$co
  }
}

Bob Rashkin
rrashkin@csc.com
 
Bob,

Thank you very much. I really appreciated your help. I got my script worked now.

thanks again.
 
Bong and everyone,

what is the best way to disable the text widget so that it won't even let you select the text and highlight it? Because even after I disabled the text widget ($widget configure -state disabled) I still can select and highlight the text and this will mess up the cursor position.

The only way I can think of is disable the mouse buttons to prevent the user from selecting the text and keep cursor in the appropriate position.

thanks.

 
I don't know how to do this. What you want is a label (or message) that supports tags. As far as I know, it doesn't exist.

Bob Rashkin
rrashkin@csc.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top