tkinter - 如何设置字体的文本? [英] tkinter - How to set font for Text?
问题描述
我试图找到在 tk.Text
中显示utf-8字符的最佳字体。
我让python使用这段代码打印所有已知的姓氏:
print(font.families(root = self。父母))
以及所有使用此代码的已知名称:
print(font.names(root = self.parent))
但是,输出族是一个字体列表,其名称由一个或多个字组成。
text = tk.Text(master = self.frame)
text.configure(font ='helvetica 12')
但是当我尝试与字体名称,其中包含多个单词,我得到一个错误:
_tkinter.TclError:期望的整数,但得到<第二个字的姓氏>
我不能设计它,因为它是一个tk而不是ttk部件,不能做:
$ $ p $ style.configure('My.TText',fontsize = 12,font ='< family name with multiple字>')
我也尝试删除姓氏的空格:
text.configure(font ='fangsongti')
但是这会导致tkinter使用一些后备字体。我检查了它输入一个名字,如:
$ $ $ code text.configure(font ='fangsongtisdngfjsbrgkrkjgbkl')
print(text。 cget('font'))
这会导致打印出我输入的字符串。所以它只是接受一切,除了多个措辞的名称。
我发现了一些字体,看起来不错,但只有一定的尺寸,我不知道他们是
#helvetica 12
#gothic 13
#mincho 13
如何设置名称由多个字组成的字体?如果我不能,具有单一措辞名称的字体适用于显示utf-8字符(例如中文(但不是唯一的)),通常字体大小的字符和在大多数情况下可用在这种方式下指定字体,使用元组:
text.configure(font =(Times New Roman,12,bold))
更好的是,您可以创建自己的自定义字体对象并按名称指定属性。注意:在创建字体对象之前,您必须先创建一个根窗口。
#python 2
#将tkinter作为tk
#从tkFont导入字体
#python 3
从tkinter.font导入tkinter为tk
导入字体
root = tk.Tk()
text = tk.Text(root )
...
myFont =字体(family =Times New Roman,size = 12)
text.configure(font = myFont)
创建自己的字体的好处是您可以稍后更改字体的任何属性,并且使用该字体的每个小部件都将自动更新。
myFont.configure(size = 14)
I am trying to find the best font for displaying utf-8 characters in a tk.Text
.
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>
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')
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
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 - 如何设置字体的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!