In this tutorial, we’ll learn how to retrieve all available fonts in Tkinter using Python code. Instead of searching for fonts online, which can be a time-consuming process, this simple code will help you get all the fonts available in Tkinter with just 10 lines of code. But before we dive into the implementation, let’s first get an overview of what Tkinter is.
Tkinter Library in python:
Tkinter is a Python library used for developing GUI (Graphical User Interface) applications. It’s comparable to the Abstract Window Toolkit (AWT) in Java; however, Tkinter in Python is considered more efficient than AWT in Java. Tkinter provides built-in tools like buttons, text boxes, checkboxes, combo boxes, radio buttons, and more. If you’re interested in exploring Tkinter projects, check out this tutorial: A simple music player GUI app using Tkinter
python code to get all available fonts in Tkinter :
import tkinter as tk # importing modules from tkinter import Tk,font window=tk.Tk() # creating a window window.geometry("700x700") # setting window size fontss=list(font.families()) # sending all fonts into a list count=1 for i in fontss: print(count,".",i) # printing the list count+=1
Output :
1 . System 2 . 8514oem 3 . Fixedsys 4 . Terminal 5 . Modern 6 . Roman 7 . Script 8 . Courier 9 . MS Serif 10 . MS Sans Serif
From the above code you can get all the Text-Fonts. You will get almost 185-186 fonts, which will print in your command prompt. You can easily copy and paste it in your font section.
Explanation of the code :
In this code, we first import the tkinter module to access its necessary functions. Then, we create a window using the tk.TK() function. After that, we set the window size to 700×700 pixels using the window.geometry(“700*700”) method.
Next, we retrieve all the available fonts in Tkinter and store them in a list usingfontss = list(font.families()). The list now contains all the fonts, which we can print using a forloop. We iterate through the list with a loop like for i in fontss , where i refers to each font name. We also initialize a counter count = 1 to keep track of the number of fonts. As each font is printed, the counter increments by 1. This way, you not only get the names of all the fonts but also their count.