Fonts 其中是font';名称字体';在蟒蛇33中?
此代码生成一个错误:Fonts 其中是font';名称字体';在蟒蛇33中?,fonts,tkinter,python-3.3,Fonts,Tkinter,Python 3.3,此代码生成一个错误: 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
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'
>>>
回溯(最近一次呼叫最后一次):
文件“C:\\ uu P\nametofont.pyw”,第4行,在
默认字体=font.nametFont(“TkDefaultFont”)
AttributeError:类型对象“Font”没有属性“nametofont”
>>>
如何访问“nametofont”?模块
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
。该类可能没有此属性。好的,我找到了需要的内容。以下是修改后的工作代码,并添加了打印语句:
from tkinter import Tk
from tkinter.font import Font, nametofont
root = Tk()
default_font = nametofont("TkDefaultFont")
print(default_font)
Font.nametofont(…)
必须是nametofont(…)
,然后它需要TK()
来获得一个窗口上下文来查看。谢谢。在我的例子中,我需要删除nametofont前面的“字体”。此调用位于一个更大的应用程序中的类中。请执行以下操作:Font.nametofont=nametofont
,这是在类中添加特性的monkeypatching,以便删除此错误。