I am trying to create a Tkinter application that uses a custom font from Google Fonts. Here is the link to the font I am using: Font Page. I have searched online for a way to change the font in Tkinter, but none of the solutions I found seem to work. Below is the most reasonable and understandable code I have found so far, but it does not seem to apply the custom font correctly.
import pyglet,tkinter
pyglet.font.add_file(r"C:\Users\Will\Downloads\Exan-Regular.ttf")
root = tkinter.Tk()
MyLabel = tkinter.Label(root,text="test",font=('Exan-Regular',25))
MyLabel.pack()
root.mainloop()
I have also tried using ChatGPT for help, but it couldn't provide a solution. Additionally, I have looked at several Stack Overflow posts, like this one, but they haven't resolved my issue either.
Can anyone help me understand what might be going wrong or provide a working example of how to use a custom Google Font in a Tkinter application?
Thank you!
For Windows 7 or above, pyglet
uses pyglet.font.directwrite.Win32DirectWriteFont
class by default which does not work with tkinter
. pyglet.font.win32.GDIPlusFont
should be used instead by setting pyglet.options['win32_gdi_font'] = True
.
Also the font name should be "Exan"
instead of "Exan-Regular"
.
import pyglet, tkinter
# use GDI font
pyglet.options['win32_gdi_font'] = True
pyglet.font.add_file("C:/Users/Will/Downloads/Exan-Regular.ttf")
root = tkinter.Tk()
# font name is "Exan"
MyLabel = tkinter.Label(root, text="Testing 0123", font=('Exan', 25))
MyLabel.pack()
root.mainloop()
Result: