我正在尝试使用python中的tkinter库构建一个文本编辑器应用程序。默认的制表符大小看起来大约是15个空格。如何将此制表符大小调整为大约4个空格?
发布于 2019-01-28 17:39:55
您可以使用Text小部件的tabs option来控制制表位的位置。用于定义宽度的选项有(从here复制)
<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.正如您所看到的,这些都不包括许多字符/空格。你可以只选择你喜欢的像素数,但是如果你想让它尽可能接近四个空格,tkinter有一种方法可以‘测量’特定字符串的宽度,如this answer所示。使用它,您可以执行以下操作:
from tkinter import *
import tkinter.font as tkfont
root = Tk()
text = Text(root)
text.pack()
font = tkfont.Font(font=text['font'])
tab = font.measure(' ')
text.config(tabs=tab)
root.mainloop()https://stackoverflow.com/questions/54394324
复制相似问题