python等宽字体怎么调_如何将Tkinter小部件设置为等宽、独立于平台的字体?

在标准字体一节中有这样的说法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难道不应该工作吗?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值