How to change a widget's font style without knowing the widget's font family/size?

Malcolm picture Malcolm · Nov 1, 2010 · Viewed 87.9k times · Source

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

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')

or

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

Answer

Bryan Oakley picture Bryan Oakley · Nov 1, 2010

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).

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.

Here's a quick example:

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()

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}