How can I change the default font of a Tkinter application during the mainloop?
I would like to have a button in my application where the user can increase or decrease the font of all the widgets. Currently, I'm just changing the default font size of root right at the beginning, but I haven't been able to modify the font size after the application is running.
I'm aware that Tkinter allows you to change the default using root.option_add("*Font", font), but how can modify the font while the application is running?
Here is an example of what I am trying to do:
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master, bg= "#E3E5E6")
self.master = master
self.grid(sticky = "NESW")
self.menubar = tk.Menu(self)
fileMenu = tk.Menu(self.menubar)
fileMenu.add_command(label="Change Font Size", command =self.changeFontSize)
self.menubar.add_cascade(label = "Click Me", menu = fileMenu)
self.master.configure(menu=self.menubar)
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
self.messageWidgetExample = tk.Message(text = text)
self.messageWidgetExample.grid()
def changeFontSize(self):
print("Change Font Size")
self.master.option_add("*Font", "calibri 8")
self.master.update()
# Doesn't work either
# self.messageWidgetExample.update()
root = tk.Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.option_add("*Font", "calibri 14")
app = Application(master=root)
app.mainloop()

