tkinter.font.font()不起作用



为什么这不起作用?我的电脑上有Consolas字体,但下面的代码只使用默认字体。似乎唯一有效的字体是安装了tkinter的Courier字体。

font_consolas = tkinter.font.Font(root, family="Consolas")

使用exists=True运行会显示以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    font_consolas = tkinter.font.Font(root, family="Consolas", exists=True)
  File "C:Python33libtkinterfont.py", line 86, in __init__
    "named font %s does not already exist" % (self.name,))
_tkinter.TclError: named font font1 does not already exist

我确实安装了字体,并通过验证

'Consolas' in tkinter.font.families() == True

指定列表中的字体有效,尽管

font_consolas = ["Consolas", ]

根据文档,如果断言(exists=True)为false,Font(...)将抛出错误。断言基于字体的名称,而不是您所基于的字体的名称。

它并不是因为Consolas字体不存在而抛出错误,而是它试图创建一个具有新名称的新字体,而该新字体不存在。由于你没有给它一个名称,它会选择一个唯一的名称,而且根据定义,一个唯一名称以前不存在,所以你会得到错误。实际上,你是在说"为我的字体创建一个唯一的名称,如果这个唯一的名称不是唯一的,就会抛出一个错误"

换句话说,它正在做它记录在案的事情。

最新更新