z33*_*33k 7 python fonts tkinter ttk monospace
据说这里的部分 标准字体:
特别是对于或多或少的标准用户界面元素,每个平台都定义了应该使用的特定字体。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.")
Run Code Online (Sandbox Code Playgroud)
我得到的是这样的:
这对我来说看起来不像是等宽的,所以我检查了在我的平台上到底Tkinter
转换TkFixedFont
成什么:
from tkinter import font
font.nametofont("TkFixedFont").actual()
Run Code Online (Sandbox Code Playgroud)
答案是:
{'family': 'DejaVu Sans Mono', 'size': 9, 'weight': 'normal', 'slant': 'roman', 'underline': 0, 'overstrike': 0}
Run Code Online (Sandbox Code Playgroud)
那么DejaVu Sans Mono
长什么样子呢?
该Tkdocs.com教程以上报价也已经在部分命名的字体,有这样说的:
名称
Courier
、Times
和Helvetica
保证受支持(并映射到适当的等宽字体、衬线字体或无衬线字体)
所以我尝试:
style.configure("Courier.TButton", font=("Courier", 16))
button.configure(style="Courier.TButton")
Run Code Online (Sandbox Code Playgroud)
现在终于得到了一个等宽的结果:
诚然,这Courier New
并不是DejaVu Sans Mono
我的平台选择作为标准等宽字体,但这至少是某种东西,对吧?但不应该TkFixedFont
只是工作吗?
标准字体(包括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')
Run Code Online (Sandbox Code Playgroud)
经测试可在 Linux 和 Windows 上使用 Python 2.7。
最重要的是,如果只"TkFixedFont"
删除周围的括号,问题中的代码将工作得很好。
归档时间: |
|
查看次数: |
6770 次 |
最近记录: |