Reputation: 1279
Is there a way to get way to get an integer to a valid Tkinter text widget position. For example, 0
would get changed to '1.0'
and if there were no characters on line 1, 1
would go to '2.0'
. I am aware of the widget's index
method however only to get the cursor position (in case this is the answer).
Many thanks
EDIT
I believe (having now done more research on the matter) that the Tk Text widget lazy loads the text (for performance). Therefore, without you getting the entire text contents and performing string actions on that, I don't think its possible (the Tk Text only has a minor understanding of what is above and below the currently viewed text - hence why a horizontal scrollbar will constantly adjust when scrolling vertically).
Upvotes: 3
Views: 5476
Reputation: 386010
The text widget supports many modifiers to an index. For example, you can take an existing index and compute a new index by adding or subtracting characters or lines. For example, if you want to get the first 10 characters on line 2 you can use "2.0 + 10 chars"
(or the more compact "2.0+10c"
). If you want the 100th character in the text widget you can use "1.0+100c"
, etc.
In addition to characters, you can add or subtract lines (eg: "end-1line"
), and you can get the start or end of a word or line (eg: "end-1c linestart"
represents the first character of the last line in the widget).
You can read about all of the forms an index can take in the canonical tcl/tk documentation: http://tcl.tk/man/tcl8.5/TkCmd/text.htm#M7
Upvotes: 5