Get a list of all available fonts in Tkinter using Python

Get a list of all available fonts in Tkinter using Python:

In this lesson, we will learn how to retrieve a list of all available fonts in Tkinter using Python.

import tkinter as tk
import tkinter.font as font
root=tk.Tk()
root.geometry("300x400")
list_box=tk.Listbox(root,width=40,height=20)
list_box.pack(side=tk.LEFT,fill=tk.BOTH,expand=True)
scrollbar=tk.Scrollbar(root,orient=tk.VERTICAL)
scrollbar.config(command=list_box.yview)
scrollbar.pack(side=tk.RIGHT,fill=tk.Y)
list_box.config(yscrollcommand=scrollbar.set)
available_fonts=font.families()
for i in available_fonts:
    list_box.insert(tk.END,i)
root.mainloop()

Output:

 

 

 

 

 

 

 

Explanation of code :

01: import tkinter as tk

  • This line imports the Tkinter library and assigns it the alias tk.
  • This library is used to create GUIs in Python.

02: import tkinter.font as font

  • This line imports the font module from tkinter.
  • This module provides functions for managing fonts within your application.

03: root=tk.Tk()

  • Creates the main window of the application. This window will be the container for all the other widgets.

04: root.geometry("300x400")

  • Sets the geometry of the main window, defining its width and height in pixels.
  • Here, it’s set to 300 pixels wide and 400 pixels high.

05: list_box=tk.Listbox(root,width=40,height=20)

  • Creates a Listbox widget named list_box.
  • This widget will display the list of fonts.
  • width=40: Sets the width of the list box to 40 characters.
  • height=20: Sets the height of the list box to 20 lines.

06: list_box.pack(side=tk.LEFT,fill=tk.BOTH,expand=True)

  • Place the list_box widget within the main window.
  • side=tk.LEFT: Positions the list box on the left side of the window.
  • fill=tk.BOTH:  The list box will expand to fill both horizontally and vertically as much space as possible.
  • expand=True: Allows the list box to further expand if the window is resized.

07: scrollbar=tk.Scrollbar(root,orient=.tk.VERTICAL)

  • Creates a Scrollbar widget named scrollbar. This scrollbar will be used to scroll through the list of fonts if there are too many to fit in the list box.
  • orient=tk.VERTICAL : Specifies that the scrollbar will be used for vertical scrolling

08: scrollbar.config(command=list_box.yview)

  • Config the scrollbar. When the user interacts with the scrollbar(e.g., clicks or drags the thumb), it will call the yview method of the list_box widget.

09: scrollbar.pack(side=tk.RIGHT,fill=tk.Y)

  • Place the scrollbar widget within the main window.
  • side=tk.RIGHT : Positions the scrollbar on the right side of the window.
  • fill=tk.Y: The scrollbar will expand vertically as much space as possible.

10: list_box.config(yscrollcommand=scrollbar.set)

  • This line establishes a two-way connection between the list box and the scrollbar.
  • yscrollbarcommand: This list box option specifies a function to be called whenever the list box needs to be scrolled vertically.
  • scrollbar.set When the list_box content needs to be scrolled, this function will be called, passing the current vertical position of the scrollbar thumb.

11. available_fonts=font.families() 

  • Retrieves a list of all the font families available on the user’s system using the families() functions from the font module and stores them in the variable available_font.

12. for i in available_fonts:

  • Starts a loop that iterates through each font family name in the available_fonts list.

13.  list_box.insert(tk.END,i)

  • Insert each font family name(I) into the list box at the end (tk, END).

14. root.mainloop()

  • Starts the main loop of the Tkinter application.
  • This method keeps the application running until the user closes the window.

In this lesson, we have successfully created a list of all available fonts in Tkinter using Python.

Leave a Comment

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

Scroll to Top