它在标准字体部分中 说:
特别是对于或多或少的标准用户界面元素,每个平台都定义了应该使用的特定字体。Tk 将其中的许多封装到一组始终可用的标准字体中,当然标准小部件使用这些字体。这有助于抽象出平台差异。
然后在预定义的字体列表中有:
TkFixedFont
A standard fixed-width font.
这也与我在此处可以找到的关于选择等宽、平台无关字体的标准方法的内容相对应,Tkinter
例如本答案中所述。
唉,当我尝试自己做这件事时,就像下面的简单代码一样:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
frame = ttk.Frame(root)
style = ttk.Style()
style.configure("Fixed.TButton", font=("TkFixedFont", 16))
button = ttk.Button(text="Some monospaced text, hopefully", style="Fixed.TButton")
frame.grid()
button.grid(sticky="news")
button.configure(text="I don't quite like this font.")
我得到的是:
这对我来说看起来不像是等宽的,所以我检查了我平台上的确切Tkinter
翻译内容:TkFixedFont
from tkinter import font
font.nametofont("TkFixedFont").actual()
答案是:
{'family': 'DejaVu Sans Mono', 'size': 9, 'weight': 'normal', 'slant': 'roman', 'underline': 0, 'overstrike': 0}
那么DejaVu Sans Mono
长什么样子呢?
上面引用的Tkdocs.com 教程也有一个关于命名字体的部分,它说:
保证支持名称
Courier
、、Times
和Helvetica
(并映射到适当的等宽、衬线或无衬线字体)
所以我尝试:
style.configure("Courier.TButton", font=("Courier", 16))
button.configure(style="Courier.TButton")
现在终于得到了等宽的结果:
诚然,我的平台Courier New
不是DejaVu Sans Mono
选择标准等宽字体,但至少是这样,对吧?但不应该TkFixedFont
只是工作吗?