问题描述
我正在尝试找到在 tk.Text
中显示 utf-8 字符的最佳字体.
I am trying to find the best font for displaying utf-8 characters in a tk.Text
.
我让 python 使用以下代码打印 tk 已知的所有姓氏:
I let python print all the family names known to tk using this code:
print(font.families(root=self.parent))
以及所有使用此代码的已知名称:
and all the known names for usages using this code:
print(font.names(root=self.parent))
但是,家族的输出是一个字体列表,其名称由一个或多个单词组成.用这样一个词很容易设置:
However the output out the families is a list of fonts, which have names consisting of one or more words. It's easy to set the ones with one word like this:
text = tk.Text(master=self.frame)
text.configure(font='helvetica 12')
但是当我尝试对由多个单词组成的字体名称进行相同操作时,我得到一个错误:
But when I try the same with the font names, which consist of multiple words, I get an error:
_tkinter.TclError: expected integer but got <second word of the family name>
我无法设置它的样式,因为它是 tk 而不是 ttk 小部件,所以很遗憾我做不到:
I can't style it, since it is a tk and not a ttk widget, so unfortunately I cannot do:
style.configure('My.TText', fontsize=12, font='<family name with multiple words>')
我还尝试像这样简单地删除姓氏的空格:
I also tried to simply remove whitespace of the family name like this:
text.configure(font='fangsongti')
但这会导致 tkinter 使用一些后备字体.我检查了它输入的名称:
But that causes tkinter to use some fallback font. I checked it entering a name like:
text.configure(font='fangsongtisdngfjsbrgkrkjgbkl')
print(text.cget('font'))
这会打印出我作为姓氏输入的确切字符串.所以它只接受所有内容,除了多个单词的名称.
And this results in printing the exact string I entered as a family name. So it simply accepts everything, except multiple worded names.
我发现了一些字体,看起来确实不错,但只有某些尺寸,我不确定它们是否适用于大多数系统:
I found some fonts, which do look OK, but only at certain sizes and I am not sure if they're available on most systems:
# helvetica 12
# gothic 13
# mincho 13
如何设置名称由多个单词组成的字体?如果我不能,哪种字体具有单字名称,适合显示 utf-8 字符,例如常见字体大小的中文(但不限于!)字符 和 在大多数情况下都可用系统?
How can I set fonts with names consisting of multiple words? If I can't, which font, having a one worded name, is appropriate for displaying utf-8 characters like for example Chinese (but not exclusively!) characters on common font sizes and is available on most systems?
推荐答案
这样指定字体时,使用元组:
When specifying fonts in this manner, use a tuple:
text.configure(font=("Times New Roman", 12, "bold"))
更好的是,您可以创建自己的自定义字体对象并按名称指定属性.注意:在创建字体对象之前,您必须首先创建一个根窗口.
Even better, you can create your own custom font objects and specify the attributes by name. Note: before you can create a font object you must first create a root window.
# python 2
# import Tkinter as tk
# from tkFont import Font
# python 3
import tkinter as tk
from tkinter.font import Font
root = tk.Tk()
text = tk.Text(root)
...
myFont = Font(family="Times New Roman", size=12)
text.configure(font=myFont)
创建自己的字体的好处是您可以稍后更改字体的任何属性,并且每个使用该字体的小部件都会自动更新.
The advantage to creating your own fonts is that you can later change any attribute of the font, and every widget that uses that font will automatically be updated.
myFont.configure(size=14)
这篇关于tkinter - 如何为文本设置字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!