Get the List of all available Font in Tkinter

In this tutorial, We will learn how to get the list of all available fonts in Tkinter. We will use Tkinter modules of Python. Tkinter is the standard GUI library in Python which helps us to make interactive and attractive user interfaces.

Requirement :

python 3.8 and above
pip install tkinter

Code :

# imported all necessary module of python 
import tkinter as tk
from tkinter import font

By, Importing the Tkinter module of Python we can create an attractive user interface. and we have imported the font class from the Tkinter modules to access the font class to get all the fonts available in Tkinter.

window = tk.Tk()
window.geometry("500x500")
window.title("FONT IN TK")

We have initialized the Tkinter module which we have imported above. then, we have set the screen size with width and height. and given a title to the page on the screen.

fonts = list(font.families())

name_label = tk.Label(window, text="FONT LIST", font=("",20,"bold"))
name_label.pack(pady=20)

name_list_box = tk.Listbox(window, width=60, height=50, font=("",15,"bold"))
name_list_box.pack()

for index, font_name in enumerate(fonts, start=1):
    name_list_box.insert(tk.END, f"{index} : {font_name}")    

we have called the families function of font class which we have imported from tkinter. then we, have provided a label to the page as FONT LIST. and created a Listbox which is a function in Tkinter that helps us create a box that contains all the text that we want, now have to iterate over the font list and insert them in the Listbox so we have to use the enumerate function of Python that will help us to provide a counter to an iterable and return it as an enumerate object with this function we have as passed Two objects first is fonts to get all the fonts and start = 1 to start the counter from 1, initially in python counter or indexing starts from 0. then we have inserted all font name in Listbox using “insert” method of Listbox function. where tk.END is used to insert all the objects till the end in the box and provide the data that we want to insert. and with the label and Listbox, we have to use a pack to insert the label text and Listbox on the screen and we can provide padding x (padx), and padding y (pady) to set the object on the screen.

window.mainloop()

This is used the close the tk window and show that the program ends.

Complete Code:

import tkinter as tk
from tkinter import font

window = tk.Tk()
window.geometry("500x500")
window.title("FONT IN TK")

fonts = list(font.families())

name_label = tk.Label(window, text="FONT LIST", font=("",20,"bold"))
name_label.pack(pady=20)

name_list_box = tk.Listbox(window, width=60, height=50, font=("",15,"bold"))
name_list_box.pack()

for index, font_name in enumerate(fonts, start=1):
    name_list_box.insert(tk.END, f"{index} : {font_name}")    

window.mainloop()

This Code helps us to get all the fonts available in Tkinter and list them on the screen for that we have imported two modules, and inserted all the fonts in the Listbox on the screen.

Output :

https://drive.google.com/file/d/1Cj7GR58mjqCpL1i03eP4M7thRie_xBKA/view?usp=sharing

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top