首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用python tkinter库时,如何调整选项卡大小?

问在使用python tkinter库时,如何调整选项卡大小?
EN

Stack Overflow用户
提问于 2019-01-28 08:56:17
回答 1查看 555关注 0票数 0

我正在尝试使用python中的tkinter库构建一个文本编辑器应用程序。默认的制表符大小看起来大约是15个空格。如何将此制表符大小调整为大约4个空格?

EN

回答 1

Stack Overflow用户

发布于 2019-01-28 17:39:55

您可以使用Text小部件的tabs option来控制制表位的位置。用于定义宽度的选项有(从here复制)

代码语言:javascript
复制
<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所示。使用它,您可以执行以下操作:

代码语言:javascript
复制
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()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54394324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档