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

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


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:

# python 2 imports# import Tkinter as tk# import tkFont# python 3 importsimport tkinter as tkimport tkinter.font as tkFontclass 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 tkimport tkFontroot = 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}


Even shorter for just one Label:

from Tkinter import *import Tkinter as tkroot = tk.Tk()# font="-weight bold" does your thingexample = Label(root, text="This is a bold example.", font="-weight bold")example.pack()root.mainloop()


just use the basic attributes of a particular widget, suppose you want to change the font of a label. You can use the following syntax:

mlabel = Label(text="Your text", font=("Name of your font",size))

this code works for python 3.4