Is There any way to use google custom fonts in Tkinter Application. I have google Font zip file from google font and I want to use that in my tkinter application.
Asked
Active
Viewed 555 times
0
-
https://stackoverflow.com/questions/11993290/truly-custom-font-in-tkinter/30631309#30631309. This should help. – suryansh_tokas Nov 29 '21 at 03:46
-
Try [`pyglet.font.add_file(font_path)`](https://pyglet.readthedocs.io/en/latest/modules/font.html#pyglet.font.add_file) from `pyglet` module. – acw1668 Nov 29 '21 at 05:36
1 Answers
0
I've came to the same problem, unfortunately it doesn't seem to be possible to use custom fonts in traditional way anyways.
But I came to a workaround by making a image with that text onto it.
def create_text_image(self, text, color_rgba_code_tuple):
fnt = Enums.ImageFont.truetype(Enums.FONT_DEFAULT, Enums.FONT_SIZE)
text_width, text_height = fnt.getsize(text)
img = Enums.Image.new('RGBA', (text_width,text_height))
d = Enums.ImageDraw.Draw(img)
d.text((0,0), text, font=fnt,fill=color_rgba_code_tuple)
img.save(Enums.FONT_TEMPORARY_IMAGE_PATH)
return Enums.PhotoImage(file=Enums.FONT_TEMPORARY_IMAGE_PATH)
Imports
from PIL import Image, ImageDraw, ImageFont, ImageTk
Explanation
You need to pass in your text and rgba tuple into the function; so, something like (255,255,255,255) for white text.
And then simply add that image that the function returns to a list, and use it in wherever place you want (like Canvas for example).
Hope this is clear.

Aleksas
- 66
- 8
-
Its not clear what `Enums` is and should be part of the answer as it is requiered for your snippet. – Thingamabobs Nov 29 '21 at 08:11
-
This is just where I store all of my imports and other variables that I don't use here. – Aleksas Nov 30 '21 at 22:16