List Available Font Families in 'Tkinter'

List available font families in `tkinter`

from tkinter import Tk, font
root = Tk()
font.families()

how to get the list of the available fonts on Tkinter?

You can do like this

from tkinter import Tk, font
root = Tk()
print(font.families())

How Tkinter font.families() function gets a list of available font?

What is the meaning of args = ('-displayof', displayof)?

Tkinter is a small python wrapper around a tcl interepreter which has the tk package installed. All of the actual work of creating widgets, querying fonts, etc, is done by the tcl/tk libraries.

Tcl/tk commands use a leading dash to represent an option name, and the option is typically followed by a value. This code is preparing to call a tcl function, so it is building up a list of arguments required by tcl.

In this specific case, it's adding the -displayof option if the displayof argument was passed to the function.

Where does tk.call() function came from?

It comes from a C-based library. It is defined in a file named _tkinter.c. More specifically, it's the function Tkapp_Call. It is a small wrapper that allows python to execute commands within an embedded tcl interpreter.

How does tk.call("font", "families", *args) function works?

The tk library has a command named font. From within a tcl interpreter you would call it with something like this:

font families

The code tk.call("font", "families", *args) is simply sending that command to the tcl interpreter.

The underlying tcl/tk library has platform-specific functions for dealing with fonts. See tkMacOSXFont.c, tkUnixFont.c, and tkWinFont.c.

Find all tkinter fonts

This outputs tkinter font names.

from tkinter import Tk
import tkinter.font
Tk()
for name in sorted(tkinter.font.families()):
print(name)

How can I find out which fonts are available in Turtle?

Try this to list fonts.

import tkinter as tk
r = tk.Tk()
print(list(tk.font.families()))

Turtle is based on tkinter, so you list the tkinter fonts.

Generate a list of all available monospaced fonts

Each font has a metrics property that tells you if the family is monospaced (or fixed-width).

This is a general piece of code that can generate all the available font families that are monospace on your machine.

from tkinter import *
from tkinter import font

if __name__ == "__main__":
root = Tk()
fonts = [font.Font(family=f) for f in font.families()]
monospace = (f for f in fonts if f.metrics("fixed"))

lbfonts = Listbox(root)
lbfonts.grid()

for f in monospace:
lbfonts.insert(END, f.actual('family'))

root.mainloop()

Unusual tkinter fonts

Tkinter does not guarantee that you have access to Times New Roman, Arial, or Calibri. It simply gives you access to whatever fonts you have on your system. The way you install fonts depends on your system. There is no way to install fonts just for tkinter.

The only thing the tk documentation says about fonts is this1:

Tk guarantees to support the font families named Courier (a monospaced “typewriter” font), Times (a serifed “newspaper” font), and Helvetica (a sans-serif “European” font). The most closely matching native font family will automatically be substituted when one of the above font families is used.


1 http://tcl.tk/man/tcl8.5/TkCmd/font.htm



Related Topics



Leave a reply



Submit