List available font families in `tkinter`
Asked Answered
R

11

35

In many tkinter examples available out there, you may see things like:

canvas.create_text(x, y, font=('Helvetica', 12), text='foo')

However, this may not work when run in your computer (the result would completely ignore the font parameter). Aparently, the font parameter is ignored if there is any incorrect value in it.

In order to check if the font family is valid, how can I list all available in my system?

Roque answered 21/9, 2016 at 10:30 Comment(1)
did you know you could also download custom fonts on your computer and use themSeigler
R
47
from tkinter import Tk, font
root = Tk()
font.families()
Roque answered 21/9, 2016 at 10:30 Comment(2)
This example doesn't work with Python 2.7. See the other answer below from Gary02127 for a Python 2 compatible version.Endocrinotherapy
It also ignores named fonts, which are worth considering, as well, such as "TkFixedFont" et alLemon
S
74

This question has been answered fully, but it's useful for me to be able to see what every font looks like so that I'm sure of which one I would like to use. In the interest of saving someone else from reinventing the wheel, I'll post my code here. As above, each font family is shown in a scrolling window. The difference is that each font in this code is printed in the font itself.

from tkinter import *
from tkinter import font

root = Tk()
root.title('Font Families')
fonts=list(font.families())
fonts.sort()

def populate(frame):
    '''Put in the fonts'''
    listnumber = 1
    for i, item in enumerate(fonts):
        label = "listlabel" + str(listnumber)
        label = Label(frame,text=item,font=(item, 16))
        label.grid(row=i)
        label.bind("<Button-1>",lambda e,item=item:copy_to_clipboard(item))
        listnumber += 1

def copy_to_clipboard(item):
    root.clipboard_clear()
    root.clipboard_append("font=('" + item.lstrip('@') + "', 12)")

def onFrameConfigure(canvas):
    '''Reset the scroll region to encompass the inner frame'''
    canvas.configure(scrollregion=canvas.bbox("all"))

canvas = Canvas(root, borderwidth=0, background="#ffffff")
frame = Frame(canvas, background="#ffffff")
vsb = Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)

vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4,4), window=frame, anchor="nw")

frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))

populate(frame)

root.mainloop()

I hope this helps somebody.

Edit: Clicking on font name will copy it to clipboard ready for use in your tkinter script, per request from @idbrii .

Steak answered 11/12, 2018 at 5:16 Comment(5)
This is great! Could only be better by making each a button that puts the corresponding font=('Helvetica', 12), text on the clipboard.Smokedry
+1, but the font "Emoji One" causes a failure here on Ubuntu. Inserting the line if not ("Emoji" in item): after line 12 (for item in fonts:) fixes it by not displaying that font. "X Error of failed request: BadLength (poly request too large or internal Xlib error)". Inserting a random emoji in the text to be displayed doesn't help (tested in case it was an issue of none of the characters in the demo string existing in that font).Scissel
Outstanding work. This is what real coding should be. So many programmers forget that coding has little to do with actual coding and more to do with the project concept. Programs need to address a meaningful and targeted need just as this one does.Washerman
@Smokedry I have updated this to allow copying the font name into your clipboard by clicking on it.Steak
@jimmiesrustled: Oh, amazing! This answer is both a great tool for working with tk and a good starting point for a font picker. Fantastic work!Smokedry
R
47
from tkinter import Tk, font
root = Tk()
font.families()
Roque answered 21/9, 2016 at 10:30 Comment(2)
This example doesn't work with Python 2.7. See the other answer below from Gary02127 for a Python 2 compatible version.Endocrinotherapy
It also ignores named fonts, which are worth considering, as well, such as "TkFixedFont" et alLemon
C
37

All The fonts Of Tkinter are:

System
Terminal
Fixedsys
Modern
Roman
Script
Courier
MS Serif
MS Sans Serif
Small Fonts
Bell Gothic Std Black
Bell Gothic Std Light
Eccentric Std
Stencil Std
Tekton Pro
Tekton Pro Cond
Tekton Pro Ext
Trajan Pro
Rosewood Std Regular
Prestige Elite Std
Poplar Std
Orator Std
OCR A Std
Nueva Std Cond
Minion Pro SmBd
Minion Pro Med
Minion Pro Cond
Mesquite Std
Lithos Pro Regular
Kozuka Mincho Pro R
@Kozuka Mincho Pro R
Kozuka Mincho Pro M
@Kozuka Mincho Pro M
Kozuka Mincho Pro L
@Kozuka Mincho Pro L
Kozuka Mincho Pro H
@Kozuka Mincho Pro H
Kozuka Mincho Pro EL
@Kozuka Mincho Pro EL
Kozuka Mincho Pro B
@Kozuka Mincho Pro B
Kozuka Gothic Pro R
@Kozuka Gothic Pro R
Kozuka Gothic Pro M
@Kozuka Gothic Pro M
Kozuka Gothic Pro L
@Kozuka Gothic Pro L
Kozuka Gothic Pro H
@Kozuka Gothic Pro H
Kozuka Gothic Pro EL
@Kozuka Gothic Pro EL
Kozuka Gothic Pro B
@Kozuka Gothic Pro B
Hobo Std
Giddyup Std
Cooper Std Black
Charlemagne Std
Chaparral Pro
Brush Script Std
Blackoak Std
Birch Std
Adobe Garamond Pro
Adobe Garamond Pro Bold
Adobe Kaiti Std R
@Adobe Kaiti Std R
Adobe Heiti Std R
@Adobe Heiti Std R
Adobe Fangsong Std R
@Adobe Fangsong Std R
Adobe Caslon Pro
Adobe Caslon Pro Bold
Adobe Arabic
Adobe Devanagari
Adobe Hebrew
Adobe Ming Std L
@Adobe Ming Std L
Adobe Myungjo Std M
@Adobe Myungjo Std M
Adobe Song Std L
@Adobe Song Std L
Kozuka Gothic Pr6N B
@Kozuka Gothic Pr6N B
Kozuka Gothic Pr6N EL
@Kozuka Gothic Pr6N EL
Kozuka Gothic Pr6N H
@Kozuka Gothic Pr6N H
Kozuka Gothic Pr6N L
@Kozuka Gothic Pr6N L
Kozuka Gothic Pr6N M
@Kozuka Gothic Pr6N M
Kozuka Gothic Pr6N R
@Kozuka Gothic Pr6N R
Kozuka Mincho Pr6N B
@Kozuka Mincho Pr6N B
Kozuka Mincho Pr6N EL
@Kozuka Mincho Pr6N EL
Kozuka Mincho Pr6N H
@Kozuka Mincho Pr6N H
Kozuka Mincho Pr6N L
@Kozuka Mincho Pr6N L
Kozuka Mincho Pr6N M
@Kozuka Mincho Pr6N M
Kozuka Mincho Pr6N R
@Kozuka Mincho Pr6N R
Letter Gothic Std
Minion Pro
Myriad Hebrew
Myriad Pro
Myriad Pro Cond
Myriad Pro Light
Rosewood Std Fill
Marlett
Arial
Arabic Transparent
Arial Baltic
Arial CE
Arial CYR
Arial Greek
Arial TUR
Batang
@Batang
BatangChe
@BatangChe
Gungsuh
@Gungsuh
GungsuhChe
@GungsuhChe
Courier New
Courier New Baltic
Courier New CE
Courier New CYR
Courier New Greek
Courier New TUR
DaunPenh
DokChampa
Estrangelo Edessa
Euphemia
Gautami
Vani
Gulim
@Gulim
GulimChe
@GulimChe
Dotum
@Dotum
DotumChe
@DotumChe
Impact
Iskoola Pota
Kalinga
Kartika
Khmer UI
Lao UI
Latha
Lucida Console
Malgun Gothic
@Malgun Gothic
Mangal
Meiryo
@Meiryo
Meiryo UI
@Meiryo UI
Microsoft Himalaya
Microsoft JhengHei
@Microsoft JhengHei
Microsoft YaHei
@Microsoft YaHei
MingLiU
@MingLiU
PMingLiU
@PMingLiU
MingLiU_HKSCS
@MingLiU_HKSCS
MingLiU-ExtB
@MingLiU-ExtB
PMingLiU-ExtB
@PMingLiU-ExtB
MingLiU_HKSCS-ExtB
@MingLiU_HKSCS-ExtB
Mongolian Baiti
MS Gothic
@MS Gothic
MS PGothic
@MS PGothic
MS UI Gothic
@MS UI Gothic
MS Mincho
@MS Mincho
MS PMincho
@MS PMincho
MV Boli
Microsoft New Tai Lue
Nyala
Microsoft PhagsPa
Plantagenet Cherokee
Raavi
Segoe Script
Segoe UI
Segoe UI Semibold
Segoe UI Light
Segoe UI Symbol
Shruti
SimSun
@SimSun
NSimSun
@NSimSun
SimSun-ExtB
@SimSun-ExtB
Sylfaen
Microsoft Tai Le
Times New Roman
Times New Roman Baltic
Times New Roman CE
Times New Roman CYR
Times New Roman Greek
Times New Roman TUR
Tunga
Vrinda
Shonar Bangla
Microsoft Yi Baiti
Tahoma
Microsoft Sans Serif
Angsana New
Aparajita
Cordia New
Ebrima
Gisha
Kokila
Leelawadee
Microsoft Uighur
MoolBoran
Symbol
Utsaah
Vijaya
Wingdings
Andalus
Arabic Typesetting
Simplified Arabic
Simplified Arabic Fixed
Sakkal Majalla
Traditional Arabic
Aharoni
David
FrankRuehl
Levenim MT
Miriam
Miriam Fixed
Narkisim
Rod
FangSong
@FangSong
SimHei
@SimHei
KaiTi
@KaiTi
AngsanaUPC
Browallia New
BrowalliaUPC
CordiaUPC
DilleniaUPC
EucrosiaUPC
FreesiaUPC
IrisUPC
JasmineUPC
KodchiangUPC
LilyUPC
DFKai-SB
@DFKai-SB
Lucida Sans Unicode
Arial Black
Calibri
Cambria
Cambria Math
Candara
Comic Sans MS
Consolas
Constantia
Corbel
Franklin Gothic Medium
Gabriola
Georgia
Palatino Linotype
Segoe Print
Trebuchet MS
Verdana
Webdings
Haettenschweiler
MS Outlook
Book Antiqua
Century Gothic
Bookshelf Symbol 7
MS Reference Sans Serif
MS Reference Specialty
Bradley Hand ITC
Freestyle Script
French Script MT
Juice ITC
Kristen ITC
Lucida Handwriting
Mistral
Papyrus
Pristina
Tempus Sans ITC
Garamond
Monotype Corsiva
Agency FB
Arial Rounded MT Bold
Blackadder ITC
Bodoni MT
Bodoni MT Black
Bodoni MT Condensed
Bookman Old Style
Calisto MT
Castellar
Century Schoolbook
Copperplate Gothic Bold
Copperplate Gothic Light
Curlz MT
Edwardian Script ITC
Elephant
Engravers MT
Eras Bold ITC
Eras Demi ITC
Eras Light ITC
Eras Medium ITC
Felix Titling
Forte
Franklin Gothic Book
Franklin Gothic Demi
Franklin Gothic Demi Cond
Franklin Gothic Heavy
Franklin Gothic Medium Cond
Gigi
Gill Sans MT
Gill Sans MT Condensed
Gill Sans Ultra Bold
Gill Sans Ultra Bold Condensed
Gill Sans MT Ext Condensed Bold
Gloucester MT Extra Condensed
Goudy Old Style
Goudy Stout
Imprint MT Shadow
Lucida Sans
Lucida Sans Typewriter
Maiandra GD
OCR A Extended
Palace Script MT
Perpetua
Perpetua Titling MT
Rage Italic
Rockwell
Rockwell Condensed
Rockwell Extra Bold
Script MT Bold
Tw Cen MT
Tw Cen MT Condensed
Tw Cen MT Condensed Extra Bold
Algerian
Baskerville Old Face
Bauhaus 93
Bell MT
Berlin Sans FB
Berlin Sans FB Demi
Bernard MT Condensed
Bodoni MT Poster Compressed
Britannic Bold
Broadway
Brush Script MT
Californian FB
Centaur
Chiller
Colonna MT
Cooper Black
Footlight MT Light
Harlow Solid Italic
Harrington
High Tower Text
Jokerman
Kunstler Script
Lucida Bright
Lucida Calligraphy
Lucida Fax
Magneto
Matura MT Script Capitals
Modern No. 20
Niagara Engraved
Niagara Solid
Old English Text MT
Onyx
Parchment
Playbill
Poor Richard
Ravie
Informal Roman
Showcard Gothic
Snap ITC
Stencil
Viner Hand ITC
Vivaldi
Vladimir Script
Wide Latin
Century
Wingdings 2
Wingdings 3
Arial Unicode MS
@Arial Unicode MS
Arial Narrow
Rupee Foradian
Rupee
DevLys 010
Calibri Light
Monoton
Ubuntu Medium
Ubuntu
Ubuntu Light
Yatra One
HelvLight
Lato
Great Vibes
Combatant answered 11/10, 2020 at 8:2 Comment(2)
What source did you use to obtain this list?Olag
this list is unique to each user though, as you can install fonts into your systemEpigraph
L
14

There are two sources of available fonts that I can think of:

  1. Font Families, and
  2. Named Fonts.
 import sys
 if sys.version_info.major == 3:
     import tkinter as tk, tkinter.font as tk_font
 else:
     import Tkinter as tk, tkFont as tk_font
 root = tk.Tk()
 print(tk_font.families())
 print(tk_font.names())

All the available, predefined font families (like 'Courier', 'Helvetica', and numerous others) should be found in the tkFont.families() call.

Named fonts are a bit different. Whenever you create a new font instance with tkFont.Font(...), you're creating a named font, and you get back the name of the new font instance. That name will show up in the tkFont.names() list. That list comes pre-filled with all the named fonts used as defaults for different widgets. If you see a font identifier you don't recognize, like 'font1234567' or 'TkFixedFont', it's probably the name of a named font, which you can easily access and use via font = tkFont.Font(name=<fontname>).

Lemon answered 21/11, 2017 at 14:47 Comment(0)
C
5

The snippet of code below will show a list of all the available fonts that Tkinter has to offer, using tkFont and the font.families().

from Tkinter import *
import tkFont

root = Tk()

fonts=list(tkFont.families())
fonts.sort()

display = Listbox(root)
display.pack(fill=BOTH, expand=YES, side=LEFT)

scroll = Scrollbar(root)
scroll.pack(side=RIGHT, fill=Y, expand=NO)

scroll.configure(command=display.yview)
display.configure(yscrollcommand=scroll.set)

for item in fonts:
    display.insert(END, item)

root.mainloop()
Carburet answered 28/5, 2017 at 17:28 Comment(1)
Please read this how-to-answer. Just include the code is not a good answer.Salangi
K
4

This is a cleaner view of all the fonts in a scroll thru window.

from tkinter import *
from tkinter import font


root = Tk()
root.title('Font Families')
fonts=list(font.families())
fonts.sort()

display = Listbox(root)
display.pack(fill=BOTH, expand=YES, side=LEFT)

scroll = Scrollbar(root)
scroll.pack(side=RIGHT, fill=Y, expand=NO)

scroll.configure(command=display.yview)
display.configure(yscrollcommand=scroll.set)

for item in fonts:
    display.insert(END, item)

root.mainloop()
Kerril answered 20/6, 2018 at 23:41 Comment(1)
It pretty much does exactly the same that https://mcmap.net/q/422777/-list-available-font-families-in-tkinter above does, isn't it? What else does this answer add?Horoscope
H
4
from tkinter import *
from tkinter import font
from os import system
root=Tk()
fonts=list(font.families())

# add  each fonts names into fonts_name_list.txt file
for font in fonts :
    system(f"echo {font} >> fonts_names_list.txt")
Hoehne answered 5/4, 2020 at 15:0 Comment(0)
N
4

You can use the code provided below to select an appropriate font.

import tkinter as tk
from tkinter import font

# Function to generate frame for displaying fonts in tkinter dialog
def generate_frm(frame):
    row = 1
    for fitem in fonts:
        if fitem != 'Noto Color Emoji':
            tk.Label(frame,
                     font=(fitem, 16, "normal"),
                     text=fitem).grid(row=row, column=1)
            row = row + 1

# Tool dialog configuration
root = tk.Tk()
root.geometry('400x600')
root.title('Font Families with Respective Style')

# Get available font families and sort them
fonts = list(font.families())
fonts.sort()

# Frame creation with scrollbar
canvas = tk.Canvas(root)
frame = tk.Frame(canvas)
scrolbr = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrolbr.set)
scrolbr.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((20,4), window=frame, anchor="nw")
frame.bind("<Configure>", lambda event, canvas=canvas: canvas.configure(scrollregion=canvas.bbox("all")))
generate_frm(frame)

root.mainloop()
Noli answered 18/6, 2022 at 6:21 Comment(1)
The original generate_frm() threw the error X Error of failed request: BadLength (poly request too large or internal Xlib length error) on Ubuntu 20.04. The problem was identified in https://askubuntu.com/questions/1236488/. The edit adds a condition to ignore the problem font, Noto Color Emoji.Estate
M
3

Here is a simple code, that will show you on screen every font name and it's appearence

import tkinter as tk 
from tkinter import *
from tkinter import Label, font

win = tk.Tk()
fonts=list(font.families())
rowcount = 0
columncount =0

for i in fonts:
    if rowcount % 30 == 0:
        columncount+=1
        rowcount = 0
    Label(win,text = i, font =(i,10,'bold')).grid(row=rowcount, column=columncount)
    rowcount+=1

win.mainloop()
Moslemism answered 2/7, 2022 at 16:21 Comment(0)
F
2

Just run this code and you will get your answer

import tkinter as tk
from tkinter import Label, ttk
from tkinter import font

class ScrollableFrame(ttk.Frame):
    def __init__(self, container, *args, **kwargs):
        super().__init__(container, *args, **kwargs)
        canvas = tk.Canvas(self)
        scrollbar = ttk.Scrollbar(self, orient="vertical", command=canvas.yview)
        self.scrollable_frame = ttk.Frame(canvas)

        self.scrollable_frame.bind(
            "<Configure>",
            lambda e: canvas.configure(
                scrollregion=canvas.bbox("all")
            )
        )

        canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")

        canvas.configure(yscrollcommand=scrollbar.set)

        canvas.pack(side="left", fill="both", expand=True)
        scrollbar.pack(side="right", fill="y")

root = tk.Tk()

fonts=list(font.families())
fonts.sort()
frame = ScrollableFrame(root)

for item in fonts:
    ttk.Label(frame.scrollable_frame,text=item,font=(item,"15")).pack()

frame.pack()
root.mainloop()
Fortna answered 5/9, 2021 at 16:19 Comment(1)
Welcome to StackOverflow. While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.Essequibo
T
0
from tkinter import Tk ,font
from os import system

#default Tk() object 
root = Tk()

#make a list of available fonts name
fonts = list(font.families())

#add each fonts name into fonts_name_list.txt file
for font in fonts :
   system(f"echo {font} >> fonts_name_list.txt")
Thule answered 22/10, 2022 at 3:47 Comment(1)
This is the same answer as two years ago.Eldrid

© 2022 - 2024 — McMap. All rights reserved.