加载tkinter的自定义字体文件时出错

时间:2016-03-25 18:17:45

标签: python windows fonts tkinter

关注问题 -
Truly custom font in Tkinter
我尝试了这段代码(根据指示更改为python 3.x的str和bytes):

FR_PRIVATE  = 0x10
FR_NOT_ENUM = 0x20
def loadfont(fontpath, private=True, enumerable=False):
    # This function was taken from
    # https://github.com/ifwe/digsby/blob/f5fe00244744aa131e07f09348d10563f3d8fa99/digsby/src/gui/native/win/winfonts.py#L15
    # This function is written for Python 2.x. For 3.x, you
    # have to convert the isinstance checks to bytes and str
    if isinstance(fontpath, bytes):
        pathbuf = create_string_buffer(fontpath)
        AddFontResourceEx = windll.gdi32.AddFontResourceExA
    elif isinstance(fontpath, str):
        pathbuf = create_unicode_buffer(fontpath)
        AddFontResourceEx = windll.gdi32.AddFontResourceExW
    else:
        raise TypeError('fontpath must be of type str or unicode')
    flags = (FR_PRIVATE if private else 0) | (FR_NOT_ENUM if not enumerable else 0)
    numFontsAdded = AddFontResourceEx(byref(pathbuf), flags, 0)
    return bool(numFontsAdded)
root = tkinter.Tk()
first =  tkinter.font.families()
print(loadfont("Interface/FONTS/BebasNeue.otf"))
second = tkinter.font.families()
print(first==second)
print([x for x in second if x not in first])
root.mainloop()

我把tkinter.font.families()放在窗口中,因为它只适用于窗口的上下文

输出结果为:

True
[]

我尝试交换实例str& amp;字节,但这也会引发错误 我到处寻找,我找不到答案。

更新

虽然字体没有显示在font.families()中,但该脚本确实有效。我的错误是假设font.families()引用环境字体并且会更新,当我认为它引用了在模块初始化时分配的变量。当我在测试窗口中将它用作字体时,它运行得很好。

0 个答案:

没有答案