A._.P._.G
A._.P._.G

Reputation: 102

How to use custom font path in tkinter? Windows ONLY

i want to use a font that is in the same directory with my python program but actually python will search windows fonts folder for it, how should i fix it?

(i want to give my program the font path or import the font to use it in my labels)

for e.g : here if the font myfont located in fonts folder my program will work but if not , it will be shown by the default font.

from tkinter import * root = Tk() my_lab = Label(root,font=("myfont" ,10 )).place(x=1,y=1)

i have use font=(f"path/to/font",10) but it doesnt work

Upvotes: 4

Views: 3052

Answers (1)

stovfl
stovfl

Reputation: 15533

Question: How to use custom font path in tkinter? Windows ONLY

Reference:


Change the function to Python 3:

def loadfont(fontpath, private=True, enumerable=False):
    ...

    # 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
    ...

Try variants of the arguments private=False, enumerable=True and make sure you have entered the font name correctly.

Run the function loadfont(...), like:

loadfont("path//to//font.ttf", private=False, enumerable=True)

Upvotes: 3

Related Questions