如何在不知道小部件的字体系列/大小的情况下更改小部件的字体样式?

Mal*_*olm 44 python user-interface fonts tkinter

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

使用案例:我们使用标准Tkinter小部件(标签,条目,文本等)创建UI .在我们的应用程序运行时,我们可能希望使用该.config()方法动态地将这些小部件的字体样式更改为粗体和/或斜体.遗憾的是,如果没有指定字体的族和大小,似乎无法指定字体规范.

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

widget.config(font='bold')
Run Code Online (Sandbox Code Playgroud)

要么

widget.config(font=( None, None, 'bold' ))
Run Code Online (Sandbox Code Playgroud)

Bry*_*ley 51

有一种比使用.config()更改应用程序字体更好的方法,特别是如果您的目标是更改整组窗口小部件(或所有窗口小部件)的字体.

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

这是一个简单的例子:

try:
    import Tkinter as tk
    import tkFont
#    import ttk  # not used here
except ImportError:  # Python 3
    import tkinter as tk
    import tkinter.font as tkFont
#    import tkinter.ttk as ttk  # not used here

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\nfont 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()
Run Code Online (Sandbox Code Playgroud)

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

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()
Run Code Online (Sandbox Code Playgroud)

当我运行上面的内容时,我得到以下输出:

{'family': 'Lucida Grande', 
 'weight': 'normal', 
 'slant': 'roman', 
 'overstrike': False, 
 'underline': False, 
 'size': 13}
Run Code Online (Sandbox Code Playgroud)

  • 在Python 3中,它是`来自tk import font`而不是`import tkFont`. (13认同)
  • 布莱恩:非常感谢!命名字体功能看起来非常方便 - 您的演示让我想起如何在浏览器中调整字体大小.在桌面客户端中执行相同操作的能力将令人印象深刻..actual()方法为我提供了解决短期任务所需的信息.给这篇文章的读者留言:.actual()返回字体高度,以像素为单位.要在构造字体元组时使用此值,必须乘以-1,因为Tkinter字体元组期望像素的字体大小为负值(正值表示以磅为单位的字体大小). (4认同)

小智 26

只有一个标签更短:

from Tkinter import *
import Tkinter as tk
root = tk.Tk()

# font="-weight bold" does your thing
example = Label(root, text="This is a bold example.", font="-weight bold")
example.pack()

root.mainloop()
Run Code Online (Sandbox Code Playgroud)


小智 7

只需使用特定小部件的基本属性,假设您要更改标签的字体。您可以使用以下语法:

mlabel = Label(text="Your text", font=("Name of your font",size))
Run Code Online (Sandbox Code Playgroud)

此代码适用于 python 3.4


Bra*_*don 6

如果您使用的是命名字体,您可以使用几个语句来获得您想要的:

import tkFont
wfont = tkFont.nametofont(widget['font'])
wfont.config(weight='bold')
Run Code Online (Sandbox Code Playgroud)

编辑以纳入 B. Oakley 的评论。

  • Corfman:除非字体是命名字体,否则这将失败。例如,定义一个像 `("Helvetica",12,"bold")` 这样的字体,你的代码就会失败。 (3认同)