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:
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?
