Tkinter.Text 索引表达式和 unicode

编程入门 行业动态 更新时间:2024-10-18 00:25:56
本文介绍了Tkinter.Text 索引表达式和 unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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.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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 07:17:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   索引   Tkinter   Text   unicode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!