Python tkinter - 如何为文本设置字体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31918073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:44:34  来源:igfitidea点击:

tkinter - How to set font for Text?

pythontextfontstkinterwidget

提问by Zelphir Kaltstahl

I am trying to find the best font for displaying utf-8 characters in a tk.Text.

我正在尝试找到在tk.Text.

I let python print all the family names known to tk using this code:

我让 python 使用以下代码打印 tk 已知的所有姓氏:

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>

I can't style it, since it is a tk and not a ttk widget, so unfortunately I cannot do:

我无法设置它的样式,因为它是 tk 而不是 ttk 小部件,所以不幸的是我不能这样做:

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')

But that causes tkinter to use some fallback font. I checked it entering a name like:

但这会导致 tkinter 使用一些后备字体。我检查它输入一个名称,如:

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

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 andis available on most systems?

如何设置名称由多个单词组成的字体?如果我不能,哪种字体具有单字名称,适合在常见字体大小上显示 utf-8 字符,例如中文(但不仅限于!)字符,并且在大多数系统上都可用?

采纳答案by Bryan Oakley

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)