让 Anaconda 的 tkinter 了解系统字体或为 Anaconda 安装新字体 [英] Make Anaconda's tkinter aware of system fonts or install new fonts for Anaconda
问题描述
我在我的 Debian Sid 笔记本上安装了两个 Python,⑴ 系统的 Python (v.2.7) 和一些实用程序包(包括 Tkinter
)和 ⑵ Anaconda 的 Python 3.
很容易看出哪些(好吧,这里有多少……)字体可用于两个 Python 发行版.
Python 2
<预><代码>>>>从 Tkinter 导入 Tk>>>从 tkFont 导入系列>>>TK();available = family() ### Tk() 需要运行 tcl 解释器<Tkinter.Tk 实例在 0x7f977bcbfb90>>>>len(可用)3011Python 3
<预><代码>>>>从 tkinter 导入 Tk>>>从 tkinter.font 导入系列>>>tk() ;可用 = 家庭()<tkinter.Tk 对象.>>>len(可用)68在我看来,Anaconda 的 tkinter
只查看基本的 X 字体随发行版一起提供请参阅下面的编辑.
你知道一个程序吗,或者
- 让 Anaconda 的
tkinter
知道系统字体(首选替代方案)或 - 在 Anaconda 的树中安装一些字体,以便
tkinter
可以使用它们?
tia
<小时>编辑Anaconda可用的字体确实是系统字体,但只有xfontsel
已知的字体,即字体路径中可以操作的字体使用 xset
.
我尝试了以下
$ cd ~/.fonts ;mkfontscale ;mkfontdir ;xset fp+`pwd`
和 xfontsel
显示了大约 30 多个字体系列.使用 Python 3 检查我确认 只有两个 字体系列被添加到可用字体列表中(即 'go'
和 'gomono'
—没有 'consolas'
等)并生成标签
<代码>...r = Tk() ;Label(r, text="Go Mono", font=('gomono', 24)).pack()
Python 2 和 Python 3 在这两种情况下都取得了成功,但 Debian 的 Python 显示了很好的抗锯齿文本,而另一个是(粗略的)位图再现.
所以,从某种意义上说,我已经部分回答了我的问题,但是
- 并非每个字体系列,如
xfontsel
所示,都被tkinter
占用 - 即使对于极少数被认可的人来说,这种演绎也有太多不尽人意的地方......
我想阅读一个更好、更有用的答案.
{tT}kinter
工作链接到 Tk/Tcl 解释器,松散地说,包含在几个 DLL 中,在特别是图形库是libtk6.0.so
.
tkinter
看不到的大多数额外字体都由 Freetype 库管理,而 Anaconda 的 libtk6.0.so
不是针对 Freetype 构建的...
$ ldd/usr/lib/x86_64-linux-gnu/libtk8.6.so |grep 自由类型libfreetype.so.6 =>/usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f0a24597000)$ ldd miniconda3/lib/libtk8.6.so |grep 自由类型$
我尝试了以下可怕的事情
$ 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$ ipythonPython 3.6.3 |Anaconda, Inc.|(默认,2017 年 11 月 20 日,20:41:42)输入版权"、信用"或许可"以获取更多信息IPython 6.2.1——增强的交互式 Python.类型 '?'求助.在 [1]: from tkinter import Tk, Label ;从 tkinter.font 导入系列在 [2]: r = Tk() ;a = 家庭();连(一)出[2]:328在 [3]: r=Tk() ;标签(r, text="Constantia", font=("Constantia", 60)).pack()在 [4] 中:r.mainloop()
最后的想法.
- 替换 DLL 不是一个干净的解决方案.
- 字体并不完全相同.当然,Anaconda 有自己的 Fontconfig 子系统,可能扫描的目录不同,但我对字体数量的差异没有正确理解.
- 正确的做法是说服 Anaconda, Inc. 针对 Freetype 构建
libtk
,但我不知道如何向他们报告,例如,如果我转到 https://www.anaconda.com/search/issues 我看到的是有关发行版的信息文章列表.
更新
关于第 3 点,我通过 一个 github 问题联系了 Anaconda Inc. 和我被告知
<块引用>不,我们不能这样做.在构建我们的软件时,我们需要很早就构建 Python,远在构建任何图形之前.添加 Freetype 作为 tkinter 的 dep 会导致构建图中出现循环,我们无法再构建发行版.
为什么不使用比 tkinter 更现代的东西呢?
--- Ray Donnelly(又名 mingwandroid)
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
- let Anaconda's
tkinter
know of the system fonts (preferred alternative) or - install a few fonts in the Anaconda's tree so that
tkinter
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
- not every font family, as shown by
xfontsel
, was taken up bytkinter
- even for the very few that were recognized, the rendition leaves too much to be desired...
and I'd like to read a better, more useful answer.
{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.
- Substituting the DLL is not a clean solution.
- The fonts are not exactly the same. For sure Anaconda has its own Fontconfig subsystem and possibly the directories that are scanned are different, but I have not a correct understanding of the discrepancy in the number of fonts.
- The correct course of action is to persuade Anaconda, Inc. to build
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.
Update
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)
这篇关于让 Anaconda 的 tkinter 了解系统字体或为 Anaconda 安装新字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!