0

Following the question-
Truly custom font in Tkinter,
I tried this code (changing to str and bytes for python 3.x as instructed):

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()

(I put tkinter.font.families() in the window as it only works in the context of a window)

The output was:

True
[]

I tried swapping the instance str & bytes but this throws an error as well.
I've looked everywhere and I can't find an answer.


UPDATE

The script actually did work, although the font did not show up in font.families(). My mistake was assuming that the font.families() was referencing the environmental fonts and would be updated, when I think it was referencing a variable assigned on the initialisation of the module. When I used it as a font in a test window, it worked perfectly.

Community
  • 1
  • 1
Tom Wright
  • 33
  • 8

0 Answers0