Tkinter.Text and unicode index expressions

Consider the following code:

from tkinter import Tk, Text

root = Tk()

text = Text(root)

text.insert("end", "abcdefgh\nґє\n一伊依医咿噫欹泆")

print(text.index("1.4+1l"), text.index("1.4+2l"))
print(text.index("3.4-1l"), text.index("3.4-2l"))

      

Some people (like me) expected it to print 2.4 3.4

and 2.4 1.4

because +1l

u -1l

should keep the column if the string is long enough. Instead, it prints 2.2 3.2

and 2.6 1.8

. It looks like it depends on the number of bytes needed to encode each character.

Should it be so? Is this documented somewhere? Should I just use something like

line, column = old_index.split(".")
new_index = text.index(f"{line+1}.{column}")

      

instead +1l

if I need to keep the columns?

+3


source to share


1 answer


The problem appears to be related to Tk, not Python:

package require Tk 8.6

pack [text .t]
.t insert end "abcdefgh\nґє\n一伊依医咿噫欹泆"

puts "[.t index 1.4+1l] [.t index 1.4+2l]"
puts "[.t index 3.4-1l] [.t index 3.4-2l]"

exit 0

      

Output:



2.2 3.2
2.6 1.8

      

So I asked the second question .

+1


source







All Articles