How to apply default tkinter font to all the tkinter windows

In the following code “font” (defined in the config file) applies only to the Tkinter window that was initiated first, in this case, “gui”. If I switch the order, it applied to the “calibration” window, but not to the “gui” window. How can I extend it to both “gui” and “calibration” windows?

gui = tk.Tk()
calibration = tk.Tk()

defaultFont = tkFont.nametofont("TkDefaultFont")
defaultFont.configure(family=font, size=9)

Answer

The two instances of Tk are completely isolated from each other. A single font configuration cannot apply to both at the same time.

Generally speaking, you should never create two instances of Tk. If you need more than one window, the second and subsequate windows should be instances of Tk. When you do that, this problem goes away.

However, if you need to create two instances of Tk then you need to configure the font for each one separately. Unfortunately, nametofont assumes only a single root window so it can’t be used. However, the body of that function is a single line of code, so we can just replicate that to supply the extra option for specifying which root window to configure.

Here is how to get and configure the default font in multiple windows:

import tkinter as tk
import tkinter.font as tkFont

gui = tk.Tk()
calibration = tk.Tk()

for window in (gui, calibration):
    defaultFont = tkFont.Font(root=window, name="TkDefaultFont", exists=True)
    defaultFont.configure(family=font, size=9)