I have two Python installations on my Debian Sid notebook, ⑴ the system's Python (v.2.7) with a little bunch of utility packages (including Tkinter
) and ⑵ Anaconda's Python 3.
It is easy to see which (well, here how many...) fonts are available for the two Python distributions.
Python 2
>>> from Tkinter import Tk
>>> from tkFont import families
>>> Tk(); available = families() ### Tk() is needed to have a running tcl interpreter
<Tkinter.Tk instance at 0x7f977bcbfb90>
>>> len(available)
3011
Python 3
>>> from tkinter import Tk
>>> from tkinter.font import families
>>> Tk() ; available = families()
<tkinter.Tk object .>
>>> len(available)
68
It seems to me that Anaconda's tkinter
only looks at the basic X fonts that came with the distributionsee edit below.
Do you know a procedure to, alternatively
tkinter
know of the system fonts (preferred alternative) ortkinter
can use them?tia
Edit the fonts available to Anaconda are indeed system fonts, but only the fonts that are known to xfontsel
, i.e., the fonts in the font path that can be manipulated using xset
.
I tried the following
$ cd ~/.fonts ; mkfontscale ; mkfontdir ; xset fp+ `pwd`
and xfontsel
showed about 30 more font families. Checking with Python 3 I verified that only two font families were added to the list of available fonts (namely 'go'
and 'gomono'
— no 'consolas'
etc) and producing a label
...
r = Tk() ; Label(r, text="Go Mono", font=('gomono', 24)).pack()
with Python 2 and Python 3 succeeded in both cases, but Debian's Python showed a nice antialiased text while the other was a (rough) bitmap rendition.
So, in a sense, I have partially answered my question, but
xfontsel
, was taken up by tkinter
and I'd like to read a better, more useful answer.
Install fonts on your system. Usually, double-click on the . ttf file and then click on the Install button in the window that pops up. Note that Matplotlib handles fonts in True Type Format (.
Font descriptorsArial (corresponds to Helvetica), Courier New (Courier), Comic Sans MS, Fixedsys, MS Sans Serif, MS Serif, Symbol, System, Times New Roman (Times), and Verdana: Note that if the family name contains spaces, you must use the tuple syntax described above.
{tT}kinter
works linking to a Tk/Tcl interpreter that, loosely speaking, is contained in a couple of DLL, in particular the graphical library is libtk6.0.so
.
Most of the extra fonts not seen by tkinter
are managed by the Freetype library and Anaconda's libtk6.0.so
is not built against Freetype...
$ ldd /usr/lib/x86_64-linux-gnu/libtk8.6.so | grep freetype
libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f0a24597000)
$ ldd miniconda3/lib/libtk8.6.so | grep freetype
$
I've tried the following, horrible thing
$ mv lib/miniconda3/lib/libtk8.6.so lib/miniconda3/lib/libtk8.6.sav
$ ln -s /usr/lib/x86_64-linux-gnu/libtk8.6.so lib/miniconda3/lib/libtk8.6.so
$ ipython
Python 3.6.3 |Anaconda, Inc.| (default, Nov 20 2017, 20:41:42)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from tkinter import Tk, Label ; from tkinter.font import families
In [2]: r = Tk() ; a = families() ; len(a)
Out[2]: 328
In [3]: r=Tk() ; Label(r, text="Constantia", font=("Constantia", 60)).pack()
In [4]: r.mainloop()
Final thoughts.
libtk
against Freetype, but I don't know how to report to them, e.g., if I go to https://www.anaconda.com/search/issues what I see is a list of informational articles on the distribution.W.r.to point 3, I contacted via a github issue Anaconda Inc. and I was told
No we cannot do this. When building our software we need python built very early, well before anything graphical gets built. Adding Freetype as a dep for tkinter causes a cycle in the build graph and we can no longer build the distro.
Why not use something more modern than tkinter anyway?
--- Ray Donnelly (aka mingwandroid)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With