如何在不知道小部件的字体系列/大小的情况下更改小部件的字体样式? [英] How to change a widget's font style without knowing the widget's font family/size?

查看:28
本文介绍了如何在不知道小部件的字体系列/大小的情况下更改小部件的字体样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不知道小部件的字体系列和字体大小的情况下更改 Tkinter 小部件的字体样式?

Is there a way to change a Tkinter widget's font style without knowing the widget's font family and font size?

用例:我们使用标准的 Tkinter 小部件(LabelEntryText 等)创建我们的 UI).当我们的应用程序运行时,我们可能希望使用 .config() 方法将这些小部件的字体样式动态更改为粗体和/或斜体.不幸的是,如果不指定字体的系列和大小,似乎无法指定字体规范.

Use case: We create our UI using standard Tkinter widgets (Label, Entry, Text, etc). While our application runs we may want to dynamically change the font style of these widgets to bold and/or italics using the .config() method. Unfortunately there appears to be no way to specify a font spec without specifying the font's family and size.

以下是我们想要做的示例,但这些示例都不起作用:

The following are examples of what we'd like to do, but neither of these examples work:

widget.config(font='bold')

widget.config(font=( None, None, 'bold' ))

推荐答案

有比使用 .config() 更好的方法来更改您的应用程序字体,尤其是当您的目标是更改字体时用于整组小部件(或所有小部件).

There's a much better way than using .config() to change your application font, especially if your goal is to change the font for a whole group of widgets (or all widgets).

Tk 真正伟大的功能之一是命名字体"的概念.命名字体的美妙之处在于,如果您更新字体,所有使用该字体的小部件都会自动更新.因此,配置您的小部件一次以使用这些自定义字体,然后更改属性是微不足道的.

One of the really great features of Tk is the notion of "named fonts". The beauty of named fonts is, if you update the font, all widgets that use that font will automatically get updated. So, configure your widgets once to use these custom fonts, then changing the attributes is trivial.

这是一个简单的例子:

# python 2 imports
# import Tkinter as tk
# import tkFont

# python 3 imports
import tkinter as tk
import tkinter.font as tkFont


class App:
    def __init__(self):
        root=tk.Tk()
        # create a custom font
        self.customFont = tkFont.Font(family="Helvetica", size=12)

        # create a couple widgets that use that font
        buttonframe = tk.Frame()
        label = tk.Label(root, text="Hello, world", font=self.customFont)
        text = tk.Text(root, width=20, height=2, font=self.customFont)
        buttonframe.pack(side="top", fill="x")
        label.pack()
        text.pack()
        text.insert("end","press +/- buttons to change
font size")

        # create buttons to adjust the font
        bigger = tk.Button(root, text="+", command=self.OnBigger)
        smaller = tk.Button(root, text="-", command=self.OnSmaller)
        bigger.pack(in_=buttonframe, side="left")
        smaller.pack(in_=buttonframe, side="left")

        root.mainloop()

    def OnBigger(self):
        '''Make the font 2 points bigger'''
        size = self.customFont['size']
        self.customFont.configure(size=size+2)

    def OnSmaller(self):
        '''Make the font 2 points smaller'''
        size = self.customFont['size']
        self.customFont.configure(size=size-2)

app=App()

如果您不喜欢这种方法,或者您想将自定义字体基于默认字体,或者您只是更改一两种字体来表示状态,则可以使用 font.actual 获取给定小部件的实际字体大小.例如:

If you don't like that approach, or if you want to base your custom font on the default font, or if you're just changing one or two fonts to denote state, you can use font.actual to get the actual size of a font for a given widget. For example:

import Tkinter as tk
import tkFont

root = tk.Tk()
label = tk.Label(root, text="Hello, world")
font = tkFont.Font(font=label['font'])
print font.actual()

当我运行上面的我得到以下输出:

When I run the above I get the following output:

{'family': 'Lucida Grande', 
 'weight': 'normal', 
 'slant': 'roman', 
 'overstrike': False, 
 'underline': False, 
 'size': 13}

这篇关于如何在不知道小部件的字体系列/大小的情况下更改小部件的字体样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆