Path to Linux Fonts in Python3 and tkinter

2019-06-27 04:51发布

问题:

By a coincidence I now have two Python 3.5 environments on my Ubuntu 16.10 Linux PC. One environment came with the Ubuntu install and I started using it until I found that Spyder was broken by the 16.10 upgrade. I installed Python 3.5 from Anaconda so that I had Spyder back. I am currently working on a program that uses box drawing characters to display data in tables which requires a mono-spaced font. It was immediately apparent that the two environments had different default fonts for a ScrolledText control. Under the Ubuntu environment the default font is: 'family': 'DejaVu Sans Mono' which works well with box drawing characters. Under Anaconda the default is: family': 'nimbus mono l' which doesn't work with box characters. I changed the font to 'lucidiatypewriter' and the box character spacing is correct but the font doesn't look very good.

I used the following script to display the available font families for the two environments.

import tkinter as tk  
import tkinter.font as tkf

root = tk.Tk()
print(tkf.families())

By comparing the list of font families to the /usr/share/fonts directory I have found that the two environments are using different paths to font files. The original Ubuntu environment is using /usr/share/fonts/truetype/... and the Anaconda environment is using /usr/share/fonts/x11/...

The Ubuntu environment presents many more choices than the Anaconda environment and the display looks much better using the truetype fonts. There has to be a configuration file somewhere that tells Python where to find fonts but I haven't been able to locate it. Does anyone know how to set Python's path to fonts?