Tkinter font property is one of the most valuable properties used to customize a widget's default font. We have already seen so many fonts and used them in our widgets, but sometimes, it seems complicated to guess which font is applicable in the Tkinter library. Python Tkinter is more specific about choosing the font. We can create an application which can list all the available font in the Tkinter library.
To use the font library, we have to import it in our environment using,
from tkinter import font
There are a few steps to create this particular application,
Define a function and create an instance of the font by using the font.families() constructor.
Iterate over all the fonts and display them using the Label Widget by assigning text values with a specific font.
Create a canvas with a vertical scrollbar.
Create a frame inside the canvas where we will display all the fonts.
Bind the Mouse Buttons to the scroll widget that allows a scroll feature in the frame.
#Import required library from tkinter import * from tkinter import font #Create an instance of tkinter frame win = Tk() win.geometry("750x350") win.title('Font List') #Create a list of font using the font-family constructor fonts=list(font.families()) fonts.sort() def fill_frame(frame): for f in fonts: #Create a label to display the font label = Label(frame,text=f,font=(f, 14)).pack() def onFrameConfigure(canvas): canvas.configure(scrollregion=canvas.bbox("all")) #Create a canvas canvas = Canvas(win,bd=1, background="white") #Create a frame inside the canvas frame = Frame(canvas, background="white") #Add a scrollbar scroll_y = Scrollbar(win, orient="vertical", command=canvas.yview) canvas.configure(yscrollcommand=scroll_y.set) scroll_y.pack(side="right", fill="y") canvas.pack(side="left", expand=1, fill="both") canvas.create_window((5,4), window=frame, anchor="n") frame.bind("<Configure>", lambda e, canvas=canvas: onFrameConfigure(canvas)) fill_frame(frame) win.mainloop()
Executing the above code will display a window that contains a list of available fonts that Tkinter supports.