0

I have a group of 10 labels in tkinter that I want to change the font size of. I know there exists option to change the font size for every single label, but I am interested to see if there is an elegant solution to changing font size of selected group of labels. Right now, I just hardcoded the size in all of them.

user430953
  • 191
  • 2
  • 19
  • if you will keep labels on list then you can use `for`-loop to change all of them. – furas Apr 12 '20 at 21:30
  • see also themed widgets [tkinter.ttk](https://docs.python.org/3/library/tkinter.ttk.html) which can use styles. You can assign the same style to all labels and change style to change all labels. – furas Apr 12 '20 at 21:32
  • every widget has function `.children` with list of widgets inside it. You can use it to find all labels (i.e. inside Window, Frame or other widget) and change them. – furas Apr 12 '20 at 21:34
  • if you want to set it only once then you can define options as dictionary ie. `options = {'bg': 'red'}` and use it in all labels as `tk.Label(root, **options)` – furas Apr 12 '20 at 21:38
  • Does this answer your question? [How to change a widget's font style without knowing the widget's font family/size?](https://stackoverflow.com/questions/4072150/how-to-change-a-widgets-font-style-without-knowing-the-widgets-font-family-siz) – stovfl Apr 12 '20 at 21:42

3 Answers3

2

Use ttk widgets. They have support for "themes" (aka "styles").

You can create a single style, and apply it to all the labels. (Example inspired by the documentation)

style = ttk.Style()
style.configure(
    "BW.TLabel",
    foreground="black",
    background="white",
    font="Helvetica",
    fontsize=12
)

l1 = ttk.Label(text="Test", style="BW.TLabel")
l2 = ttk.Label(text="Test", style="BW.TLabel")
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • I have tried a simplified version as `ttk.Style().configure('heading.TLabel',fontsize=48)`, without and without the `font=` part. I can’t get this to change the font size. – Manngo Sep 08 '21 at 06:45
2

You can define options as dictionary and use this dictionary with every widget

import tkinter as tk

root = tk.Tk()

options = {'font': (None, 20)}

l = tk.Label(root, text="Label 1", **options)
l.pack()

l = tk.Label(root, text="Label 2", **options)
l.pack()

l = tk.Label(root, text="Label 3", **options)
l.pack()

root.mainloop()

If you will keep labels on list then you can use for-loop to change them

import tkinter as tk

root = tk.Tk()

all_labels = []

l = tk.Label(root, text="Label 1")
l.pack()
all_labels.append(l)

l = tk.Label(root, text="Label 2")
l.pack()
all_labels.append(l)

l = tk.Label(root, text="Label 3")
l.pack()
all_labels.append(l)

for l in all_labels:
    l['font'] = (None, 20)

root.mainloop()

Every widget keep information about children widgets and you can use it to find all Labels

import tkinter as tk

root = tk.Tk()

l = tk.Label(root, text="Label 1")
l.pack()

l = tk.Label(root, text="Label 2")
l.pack()

l = tk.Label(root, text="Label 3")
l.pack()

for widget in root.winfo_children():
    if isinstance(widget, tk.Label):
        widget['font'] = (None, 20)

root.mainloop()

In Roland Smith answer you can see how to use tkinter.ttk

furas
  • 134,197
  • 12
  • 106
  • 148
0

Here an elegant oneliner that sets the font for all labels in your root (mainwindow) to font size 30

[wid.config(font=(None,30)) for wid in root.winfo_children() if isinstance(wid, Label) ]

In a many liner
You can also iterate over all widgets in your main window, filter out all Labels and then change there properties with a loop

# Iterate through all widgets of your main window / root
for wid in root.winfo_children():
    # check if it's a label
    if isinstance(wid, Label):
        wid.config(font=(None,30))
Björn
  • 1,610
  • 2
  • 17
  • 37