3

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

stovfl
  • 14,998
  • 7
  • 24
  • 51
A._.P._.G
  • 92
  • 1
  • 10
  • 2
    Does this answer your question? [Truly custom font in Tkinter](https://stackoverflow.com/questions/11993290/truly-custom-font-in-tkinter) – stovfl Feb 16 '20 at 13:09
  • actually it just says to use photo instead , and because my gui program has a lot of window inside each other i cant use photo in it unless i write it with class wich i dont know how to and actually i dont want to make it editable , you know if anyone change the photo my program will compeletly change – A._.P._.G Feb 16 '20 at 15:15
  • ***"just says to use photo instead"***: Have you read the last comment also? – stovfl Feb 16 '20 at 15:44
  • oh yes sorry , and can you explain how should i use this function i run function `loadfont("path\\to\\font")` but it will come up with a Type Error – A._.P._.G Feb 16 '20 at 15:55
  • ***"come up with a Type Error"***: Have you obey the comment: **This function is written for Python 2.x.***. Which version you are on. – stovfl Feb 16 '20 at 15:59
  • I've change it to python3.0 now and it works properly with out any error but when i run my program the text are shown by the default font. – A._.P._.G Feb 17 '20 at 12:15
  • Did your font show up using: `tkinter.font.families()` – stovfl Feb 17 '20 at 12:18
  • no it doesn't , it just shoo my fonts that e located in windows font folder and some other – A._.P._.G Feb 17 '20 at 15:58
  • To verify for sure, compare the output from `.font.families()` **without** `loadfont` and then **with** `loadfront`. Relevant: [designating-a-font-in-tkinter](https://stackoverflow.com/questions/35971184) – stovfl Feb 17 '20 at 16:00
  • no difference , i don't get what exactly loadfont() do , and i dont get how to use it , i think im doing wrong , i just call it like this `fontload("path\\to\\font.tff")` – A._.P._.G Feb 17 '20 at 16:10
  • 1
    ***`fontload("path\\to\\font.tff")`***: Should be `.ttf`. Try variants of the arguments `private=False, enumerable=True` – stovfl Feb 17 '20 at 16:23
  • 1
    yes now its working , thank you very much , how should i accept your comment to be shown to others as answer? – A._.P._.G Feb 18 '20 at 11:55

1 Answers1

2

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)
Community
  • 1
  • 1
stovfl
  • 14,998
  • 7
  • 24
  • 51
  • yes , actually it've solved and shown correctly but texts size is changed. is it related to compile? its ok without compiling – A._.P._.G Feb 18 '20 at 14:46
  • ***"is it related to compile? "***: It's common issue that *compiled* versions behave different. They are often missing related files and fallback to defaults or fail in whole. Have you doublechecked, your `.ttf` files packed and used by `loadfont(...` exact the same? Please edit the answer to show your used `loadfont(...` arguments. – stovfl Feb 18 '20 at 14:54
  • yes they are the same and they are also ok on my pc but when i run my program on the other computer the problem shown. what should i edit in the answer you've edited it – A._.P._.G Feb 18 '20 at 15:14