修改 Python Tkinter 中的默认字体 [英] Modify the default font in Python Tkinter
问题描述
我正在使用 Tkinter 使用 Python2.7 开发 GUI,但我遇到了一个烦人的问题.
I'm working on a GUI in Python2.7, with Tkinter, and I have an annoying problem.
如果可能,我想在一行中定义所有小部件使用的默认字体.此行仅修改 Entry 或 ComboBox 中使用的字体:
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:
root.option_add("*Font", "courier 10")
但不是示例复选框的标签.
but not the label of checkbox by example.
我发现存在预定义字体TkDefaultFont",但我无法更改其配置:
I found that a predefined font exist "TkDefaultFont" but I'm unable to change its configuration:
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()
返回:
{'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 !!)
我做错了什么?
推荐答案
Tkinter 有几种内置字体——TkDefaultFont
、TkTextFont
、TkFixedFont
等.这些都是所谓的命名字体".它们非常强大——改变其中之一,所有使用它们的小部件也会改变.
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.
要更改其中一种字体,请获取它的句柄,然后使用 configure
方法进行更改.例如,要将 TkDefaultFont
的大小更改为 48,您可以这样做:
To change one of these fonts, get a handle to it and then use the configure
method to change. For example, to change the size of TkDefaultFont
to 48 you would do this:
default_font = tkFont.nametofont("TkDefaultFont")
default_font.configure(size=48)
就是这样.您无需执行任何其他操作 - 使用 TkDefaultFont
的所有内容都会立即注意到更改.
That's it. You don't have to do anything else -- everything that uses TkDefaultFont
will instantly notice the change.
在您的问题中,您暗示您希望所有内容都使用 TkDefaultFont
字体.为此,您可以使用 option_add
如您所示:
In your question you imply you want TkDefaultFont
font to be used by everything. To do that you can use option_add
as you've shown:
root.option_add("*Font", default_font)
但是请注意,option_add
仅影响在您调用 option_add
之后 创建的小部件,因此您需要在创建任何其他小部件.
Note, however, that option_add
only affects widgets created after you've called option_add
, so you need to do it before creating any other widgets.
另请注意,如果您不想首先获取字体实例,您可以将字体名称指定给 option_add
(即:root.option_add("*Font","TkDefaultFont")
).
Also note that you can give the font name to option_add
if you don't want to bother with getting the font instance first (ie: root.option_add("*Font", "TkDefaultFont")
).
这篇关于修改 Python Tkinter 中的默认字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!