Problem with setting the cursor in a text widget
- From: mike.peattie@xxxxxxxxx
- Date: 31 Jan 2007 13:43:06 -0800
I have a text widget that I read a file's contents into, and I want to
set the input cursor to always start at the beginning of line 2 (2.0)
and not the default (1.0). The problem is, when I change the file I
have open and set the cursor again, an extra line is inserted at
position 2.0. Here's the code:
package require Tk
set cursorXLoc 0
set cursorYLoc 1
set regularFont {arial 24 roman}
set highlightFont {arial 24 roman bold}
set doHighlight 1
proc findCursorLoc { textArea } {
global cursorXLoc cursorYLoc
update
foreach {key value index} [$textArea dump -mark 1.0 end] {
if {[string compare $value "insert"] == 0} {
set loc [split $index .]
set cursorXLoc [lindex $loc 1]
set cursorYLoc [lindex $loc 0]
set cmd "*FOUND* cursor location: $cursorYLoc.$cursorXLoc "
}
}
update
}
proc highlightAndScroll { textArea scrollAmount } {
global cmd cursorYLoc highlightFont doHighlight
set endLoc [$textArea index end]
set textView [$textArea yview]
set topLoc 0
if {$cursorYLoc > 9 } {
set topLoc [expr (double($cursorYLoc) - 9)/double($endLoc)]
#set cmd "$topLoc"
}
if { $cursorYLoc < $endLoc } {
$textArea tag delete line
if { $doHighlight == 1 } {
$textArea tag add line $cursorYLoc.0 "[expr $cursorYLoc + 1].
0"
$textArea tag configure line -background red -font
$highlightFont
$textArea yview moveto $topLoc
} else {
$textArea yview scroll $scrollAmount units
}
}
update
}
proc readFile { filename } {
set myFile [open $filename "r"]
.f.t delete 1.0 end
.f.t insert 1.0 [read $myFile]
.f.t mark set insert 2.0
findCursorLoc .f.t
highlightAndScroll .f.t 0
}
pack [frame .f] -fill both -expand 1
set textArea [text .f.t -bg black -fg white]
pack $textArea -fill both -expand 1
readFile "test.txt"
bind $textArea <Control-o> {
set types {
{{Text Files} {.txt} }
}
set filename [tk_getOpenFile -filetypes $types -defaultextension
txt]
if {[string compare $filename ""] != 0} {
readFile $filename
}
}
Just have test.txt have something like this:
line 1
line3
line4
line6
Then try starting the script, hitting ctrl-o, and open the same text
file again. Extra Inserted line! What am I doing wrong?
Thanks,
Mike
.
- Prev by Date: Re: Pop-up menu from canvas?
- Previous by thread: running a tcl script that calls into C# code
- Index(es):