在标准字体一节中说是这里
特别是对于或多或少的标准用户界面元素,每个平台都定义了应该使用的特定字体。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
(并映射到适当的单空间、serif或sans-serif字体)。
所以我试着:
style.configure("Courier.TButton", font=("Courier", 16))
button.configure(style="Courier.TButton")
现在我终于得到了一个单一的结果:
诚然,我的平台选择的是Courier New
而不是DejaVu Sans Mono
作为标准的单频字体,但这至少是有意义的,对吧?但TkFixedFont
不应该只是起作用吗?
发布于 2018-07-04 21:21:36
标准字体(包括TkFixedFont
)只能作为普通字符串,而不是元组。也就是说,font='TkFixedFont'
工作,而font=('TkFixedFont',)
(请注意括号和逗号)不能工作。
这似乎是一般情况。我和Tkinter.Button
和ttk.Style
都试过了。
就风格而言,这意味着:
import Tkinter
import ttk
# will use the default (non-monospaced) font
broken = ttk.Style()
broken.configure('Broken.TButton', font=('TkFixedFont', 16))
# will work but use Courier or something resembling it
courier = ttk.Style()
courier.configure('Courier.TButton', font=('Courier', 16))
# will work nicely and use the system default monospace font
fixed = ttk.Style()
fixed.configure('Fixed.TButton', font='TkFixedFont')
测试如何在Linux和Windows上使用Python2.7。
底线是,只要去掉"TkFixedFont"
周围的括号,问题中的代码就会运行得很好。
https://stackoverflow.com/questions/48731746
复制相似问题