I want to download an open source font and use it in my Python Tkinter Program.
How can I tell Tkinter to import a font from a directory or by putting the font in the same folder as the program ?
Note: I have searched for an answer for a while even reading API reference of Tkinter about every font related thing I could find. If there was an obvious answer to this question and I didn't know because maybe I didn't search hard enough or asked the wrong questions I am sorry.
Install fonts on your system. Usually, double-click on the . ttf file and then click on the Install button in the window that pops up. Note that Matplotlib handles fonts in True Type Format (.
In order to work with a tkinter application, we have to install and import the tkinter library in our environment. Generally, we import the tkinter library in the environment by using from tkinter import * command. The significance of "import *" represents all the functions and built-in modules in the tkinter library.
This worked for me on Windows (and I would guess on any platform which supports the pyglet module)
import tkinter as tk
import pyglet, os
pyglet.font.add_file('myFont.ttf') # Your TTF file name here
root = tk.Tk()
MyLabel = tk.Label(root,text="test",font=('myFont',25))
MyLabel.pack()
root.mainloop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With