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
How to Clear Only Last One Line in Python Output Console
How to Perform Union on Two Dataframes With Different Amounts of Columns in Spark
Checking If a Button Has Been Pressed in Python
Removing Backslashes from a String in Python
How to Remove Words in a Column in Pandas
Find Specific Words in Text File and Print the Line Using Python
How to Limit the User Input to Only Integers in Python
Python - How to Make User Input Not Case Sensitive
Counting Non Zero Values in Each Column of a Dataframe in Python
How to Kill a While Loop With a Keystroke
How to Remove Any Url Within a String in Python
Deal With Overflow in Exp Using Numpy
How to Determine If My Python Shell Is Executing in 32Bit or 64Bit
How to Run Python Script from Another Machine Without Installing Imported Modules
No Output Displays When Execute Python File
How to Make a Function Change Variables While in a While Loop
Get the Row(S) Which Have the Max Value in Groups Using Groupby
How to Check All Versions of Python Installed on Osx and Centos