问题描述
限时送ChatGPT账号..让我们考虑以下代码:
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.4
和 2.4 1.4
,因为 +1l
和 -如果行足够长, 1l
应该保留该列.相反,它打印 2.2 3.2
和 2.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屋!
更多推荐
[db:关键词]
发布评论