修改 Python Tkinter 中的默认字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15462647/
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-18 20:12:46  来源:igfitidea点击:

Modify the default font in Python Tkinter

pythonpython-2.7tkinter

提问by ericc

I'm working on a GUI in Python2.7, with Tkinter, and I have an annoying problem.

我正在使用 Tkinter 在 Python2.7 中处理 GUI,但我遇到了一个烦人的问题。

I would like to define the default font used by all the widgets, if possible in one line. This line modify only the font used in Entry, or ComboBox:

如果可能,我想在一行中定义所有小部件使用的默认字体。此行仅修改 Entry 或 ComboBox 中使用的字体:

root.option_add("*Font", "courier 10")

but not the label of checkbox by example.

但不是例如复选框的标签。

I found that a predefined font exist "TkDefaultFont" but I'm unable to change its configuration:

我发现存在预定义的字体“TkDefaultFont”,但我无法更改其配置:

print tkFont.Font(font='TkDefaultFont').configure()
tkFont.Font(font='TkDefaultFont').config(family='Helvetica', size=20)
tk.TkDefaultFont = tkFont.Font(family="Helvetica",size=36,weight="bold")
print tkFont.Font(font='TkDefaultFont').configure()

return :

返回 :

{'family': 'DejaVu Sans', 'weight': 'normal', 'slant': 'roman', 'overstrike': 0, 'underline': 0, 'size': -12} {'family': 'DejaVu Sans', 'weight': 'normal', 'slant': 'roman', 'overstrike': 0, 'underline': 0, 'size': -12}

{'family': 'DejaVu Sans', 'weight': 'normal', 'slant': 'roman', 'overstrike': 0, 'underline': 0, 'size': -12} {'family': 'DejaVu Sans', 'weight': 'normal', 'slant': 'roman', 'overstrike': 0, 'underline': 0, 'size': -12}

(no errors, but nothing change !!)

(没有错误,但没有任何变化!!)

What I'm doing wrong ?

我做错了什么?

采纳答案by Bryan Oakley

Tkinter has several built-in fonts -- TkDefaultFont, TkTextFont, TkFixedFont, etc. These are all what are called "named fonts". They are remarkably powerful -- change one of these and all widgets that use them will change as well.

Tkinter 有几种内置字体—— TkDefaultFontTkTextFontTkFixedFont等。这些都是所谓的“命名字体”。它们非常强大——改变其中之一,所有使用它们的小部件也会改变。

To change one of these fonts, get a handle to it and then use the configuremethod to change. For example, to change the size of TkDefaultFontto 48 you would do this:

要更改其中一种字体,请获取它的句柄,然后使用该configure方法进行更改。例如,要将 的大小更改TkDefaultFont为 48,您可以这样做:

default_font = tkFont.nametofont("TkDefaultFont")
default_font.configure(size=48)

That's it. You don't have to do anything else -- everything that uses TkDefaultFontwill instantly notice the change.

就是这样。您不必做任何其他事情——使用的所有东西TkDefaultFont都会立即注意到变化。

In your question you imply you want TkDefaultFontfont to be used by everything. To do that you can use option_addas you've shown:

在您的问题中,您暗示您希望TkDefaultFont所有内容都使用字体。为此,您可以使用option_add如下所示:

root.option_add("*Font", default_font)

Note, however, that option_addonly affects widgets created afteryou've called option_add, so you need to do it before creating any other widgets.

但是请注意,这option_add仅影响您调用之后创建的小部件option_add,因此您需要在创建任何其他小部件之前执行此操作。

Also note that you can give the font name to option_addif you don't want to bother with getting the font instance first (ie: root.option_add("*Font", "TkDefaultFont")).

另请注意,option_add如果您不想首先获取字体实例(即:),则可以将字体名称指定为root.option_add("*Font", "TkDefaultFont")

回答by Gary02127

Caveat: although the question involves Py2.7, my answer is for Py3. The concepts are exactly the same. But instead of tkinter.font, one would use tkFontfor Py2, etc.

警告:虽然问题涉及 Py2.7,但我的回答是针对 Py3。概念完全一样。但不是tkinter.font,而是tkFont用于 Py2 等。

If you want to change a default font, or any named font, you have to access the font object via nametofont():

如果要更改默认字体或任何命名字体,则必须通过以下方式访问字体对象nametofont()

def_font = tkinter.font.nametofont("TkDefaultFont")

and then config the returned object, like

然后配置返回的对象,比如

def_font.config(size=24)

When you call

你打电话时

myfont = tkinter.font.Font(font="TkDefaultFont")

you're actually creating a new named font that has the same attributes. To help show this:

您实际上是在创建具有相同属性的新命名字体。为了帮助显示这一点:

str(def_font)gives "TkDefaultFont", and

str(def_font)给出“TkDefaultFont”,和

str(myfont)gives "font1"

str(myfont)给出“font1”

Oops.. Forgot to mention... You asked what you were doing wrong. One of the things is in your second line, you create and then config a new named font, but you don't capture it into a variable. If you captured it, you could use that named font. But that still would not modify the default named fonts. You would have to use nametofont()as explained above to accomplish that.

哎呀..忘了提...你问你做错了什么。其中一件事是在您的第二行中,您创建并配置一个新的命名字体,但您没有将其捕获到变量中。如果您捕获了它,则可以使用该命名字体。但这仍然不会修改默认的命名字体。您必须nametofont()按照上面的说明使用才能完成此操作。