tkinter选项卡宽度不正确

发布于 2025-01-26 17:09:35 字数 914 浏览 2 评论 0原文

当使用create_text方法在画布上创建文本时,选项卡的宽度不是应有的,如font.measure所示。

import tkinter as tk
from tkinter.font import Font

root = tk.Tk()

canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()

font = Font(family='Arial', size=12)

s1 = "a\tb"
s2 = "a    c"
print("Width:", s1, font.measure(s1))  # Width: a        b 30
print("Width:", s2, font.measure(s2))  # Width: a    c 33

canvas.create_text(10, 10, text=s1, font=font, anchor="nw")
canvas.create_text(10, 50, text=s2, font=font, anchor="nw")

root.mainloop()

font.measure的结果表明,带有空格的行应该更长一些,但是它显示的是:

“

表明该宽度的宽度标签明显大于空间。使用不同的字体将导致大小不同的选项卡,但仍不准确的测量值。没有标签的文本的测量宽度是正确的。

如何获得正确的标签宽度?这是一个错误吗?

When creating text on a canvas using the create_text method the width of a tab is not what it should be, as indicated by font.measure.

import tkinter as tk
from tkinter.font import Font

root = tk.Tk()

canvas = tk.Canvas(root, width=300, height=300)
canvas.pack()

font = Font(family='Arial', size=12)

s1 = "a\tb"
s2 = "a    c"
print("Width:", s1, font.measure(s1))  # Width: a        b 30
print("Width:", s2, font.measure(s2))  # Width: a    c 33

canvas.create_text(10, 10, text=s1, font=font, anchor="nw")
canvas.create_text(10, 50, text=s2, font=font, anchor="nw")

root.mainloop()

The results of font.measure suggest that the line with spaces should be a little longer, but what it displays is:

Width of tab is larger than width of spaces

Showing that the width of the tab is significantly larger than the spaces. Using different fonts will result in differently sized tabs, but still inaccurate measurements. The measured width of the text without tabs is correct.

How can I get the correct tab width? Is this a bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫腻 2025-02-02 17:09:35

您突出显示的问题是由于没有tabs属性的帆布文本对象。

也许有人知道如何解决这个问题,但是通常在显示列表时使用tk.text对象,其中具有fonttabs属性。

因此,最简单的解决方案是制作tk.text对象t,定义字体和选项卡,
将您的消息插入t,然后将t插入canvas窗口w

import tkinter as tk
from tkinter import font

app = tk.Tk()

fixed = 1 # test Arial & Courier New
FONT = font.Font(family = ["arial", "courier new"][fixed], size = 12)
wide = 4 * FONT.measure(0)
high = FONT.metrics("linespace")
dent = (wide, tk.LEFT)

canvas = tk.Canvas(app)
canvas.grid(sticky =tk.NSEW)

s1, s2 = "a\tb\n", "a   c"

T = tk.Text(
    canvas,
    background = "SystemButtonFace",
    relief = tk.FLAT,
    font = FONT,
    tabs = dent,
    width = 20,
    height = 3,
    highlightthickness = 0,
    borderwidth = 0)

W = canvas.create_window(wide, high, window = T, anchor = tk.NW)

T.insert("1.0", s1)
T.insert("insert", s2)

app.mainloop()

The problem you've highlighted is due to the Canvas text object not having a tabs attribute.

Maybe someone knows how to work around that but usually when displaying tabulation a tk.Text object is used which has font and tabs attributes.

So the easiest solution is to make a tk.Text object T, define font and tabs,
insert your messages into T then insert T into a Canvas window W.

Something like this.

import tkinter as tk
from tkinter import font

app = tk.Tk()

fixed = 1 # test Arial & Courier New
FONT = font.Font(family = ["arial", "courier new"][fixed], size = 12)
wide = 4 * FONT.measure(0)
high = FONT.metrics("linespace")
dent = (wide, tk.LEFT)

canvas = tk.Canvas(app)
canvas.grid(sticky =tk.NSEW)

s1, s2 = "a\tb\n", "a   c"

T = tk.Text(
    canvas,
    background = "SystemButtonFace",
    relief = tk.FLAT,
    font = FONT,
    tabs = dent,
    width = 20,
    height = 3,
    highlightthickness = 0,
    borderwidth = 0)

W = canvas.create_window(wide, high, window = T, anchor = tk.NW)

T.insert("1.0", s1)
T.insert("insert", s2)

app.mainloop()
七秒鱼° 2025-02-02 17:09:35

这会有所帮助吗?在第12行中,我刚刚添加了\ t

s2 = "a\tc"

结果:

”在此处输入图像描述”

Does this will help? In line 12, I just added \t

s2 = "a\tc"

Result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文