How to get the list of all available fonts in Tkinter
In this tutorial, we will learn how to get the list of all available fonts in Tkinter.
We have to import font module like this:
from tkinter import Tk, font
font.families()
will return all the font names available in Tkinter. So we can easily store those in a list variable. Once done, then we can simply print it on our console or terminal if we wish. (Note that you must create a window first)
Print all available font names in Tkinter:
Let’s understand through and example:
import tkinter as tk from tkinter import Tk, font newWindow = tk.Tk() all_fonts = list(font.families()) print(all_fonts)
If you run it and take a look at your console or terminal, you will see all the font names are printed in a list.
If you want to give a line space you can use for loop in Python to print each font name in a single line just like this:
import tkinter as tk from tkinter import Tk, font newWindow = tk.Tk() all_fonts = list(font.families()) for font_name in all_fonts: print(font_name)
Output will be like this:
Academy Engraved LET Adelle Sans Devanagari AkayaKanadaka AkayaTelivigala Al Bayan Al Nile .... and so on.
Count the number of available fonts in Tkinter:
We can use len function to count the length of the list.
import tkinter as tk from tkinter import Tk, font newWindow = tk.Tk() all_fonts = list(font.families()) print(len(all_fonts))
Output:
import tkinter as tk from tkinter import Tk, font newWindow = tk.Tk() all_fonts = list(font.families()) print(len(all_fonts))
Output:
310
Leave a Reply