此代码生成错误:
import tkinter
from tkinter.font import Font, nametofont
default_font = Font.nametofont("TkDefaultFont")
错误是:
Traceback (most recent call last):
File "C:\__P\nametofont.pyw", line 4, in <module>
default_font = Font.nametofont("TkDefaultFont")
AttributeError: type object 'Font' has no attribute 'nametofont'
>>>
如何访问'nametofont'?
答案 0 :(得分:2)
好的,我想出了我需要的东西。这是修改后的工作代码,附带一个打印声明:
from tkinter import Tk
from tkinter.font import Font, nametofont
root = Tk()
default_font = nametofont("TkDefaultFont")
print(default_font)
Font.nametofont(...)
只需要nametofont(...)
,然后需要TK()
才能获得要查看的窗口上下文。
答案 1 :(得分:0)
模块tkinter.font
# Tkinter font wrapper
#
# written by Fredrik Lundh, February 1998
#
__version__ = "0.9"
import itertools
import tkinter
# weight/slant
NORMAL = "normal"
ROMAN = "roman"
BOLD = "bold"
ITALIC = "italic"
def nametofont(name):
"""Given the name of a tk named font, returns a Font representation.
"""
return Font(name=name, exists=True)
可能需要写font.nametofont
而不是Font.nametofont
。该类可能没有此属性。