在标准字体一节中有这样的说法here:Particularly for more-or-less standard user interface elements, each
platform defines specific fonts that should be used. Tk encapsulates
many of these into a standard set of fonts that are always available,
and of course the standard widgets use these fonts. This helps
abstract away platform differences.
然后在“预定义字体”列表中有:TkFixedFontA standard fixed-width font.
这也与我在这里找到的在Tkinter中选择等距、独立于平台的字体的标准方法相一致,例如this answer。在
唉,当我自己尝试这样做时,就像下面的简单代码: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转换成什么:
^{pr2}$
答案是:{'family': 'DejaVu Sans Mono', 'size': 9, 'weight': 'normal', 'slant': 'roman', 'underline': 0, 'overstrike': 0}
那么DejaVu Sans Mono是什么样子的呢?在
Tkdocs.com网站上面引用的教程也有一节是关于命名字体的,上面写着:the names Courier, Times, and Helvetica are guaranteed to be supported
(and mapped to an appropriate monospaced, serif, or sans-serif font)
所以我试着:style.configure("Courier.TButton", font=("Courier", 16))
button.configure(style="Courier.TButton")
现在我得到了一个等距的结果:
诚然,我的平台选择Courier New而不是{}作为标准的等距字体,但至少这是一些东西,对吧?但是TkFixedFont难道不应该工作吗?在