Tkinter.Text 索引表达式和 unicode [英] Tkinter.Text index expressions and unicode

查看:29
本文介绍了Tkinter.Text 索引表达式和 unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑以下代码:

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"))

有些人(例如我)会希望它打印 2.4 3.42.4 1.4,因为 +1l-如果行足够长, 1l 应该保留该列.相反,它打印 2.2 3.22.6 1.8.看起来这取决于编码每个字符所需的字节数.

Some people (for example me) would expect it to print 2.4 3.4 and 2.4 1.4, because +1l and -1l are supposed to preserve the column if the line 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 this way? Is it documented somewhere? Should I just use something like

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

如果我关心保留的列,而不是 +1l?

instead of +1l if I care about columns being preserved?

推荐答案

问题似乎与 Tk 相关,与 Python 无关:

The problem seems to be Tk-related, not Python-related:

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

输出:

2.2 3.2
2.6 1.8

所以我问了一个第二个问题.

这篇关于Tkinter.Text 索引表达式和 unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆