Tkinter 文本小部件设置选项卡

use*_*473 4 python tkinter

我必须将文本小部件的标签大小设置为 4 个字符。当我这样做时, textwidget.config(tabs = ("4c","8c"))我没有得到所需的结果。它的标签很多,现在是 4 个字符。设置 tabs 属性时我做错了什么吗?此外,当我显示行号和列号时,我通过获取插入的索引来实现,但是这里的制表符被标记为一个字符而不是四个我希望它是理想的字符。有没有更好的方法来获取列号而不必处理制表符的这个问题?

pat*_*yts 5

Tk 文本小部件-tabs选项的文档提到 2c 的距离为 2 厘米。文档似乎没有说,但这使用Tk_GetPixels函数将您的选项值转换为距离,并在此处说明以下类型:

<none> : The number specifies a distance in pixels.
c : The number specifies a distance in centimeters on the screen.
i : The number specifies a distance in inches on the screen.
m : The number specifies a distance in millimeters on the screen.
p : The number specifies a distance in printer's points (1/72 inch) on the screen.
Run Code Online (Sandbox Code Playgroud)

如果您想在字符中使用距离,那么您应该使用字体测量,使用为您的文本小部件定义的字体和合适的字符,例如mn给定比例字体字符具有不同的宽度。Tk text widget -tabs 文档中给出了一个示例。