Modify the default font in Python Tkinter


In order to change the default behavior of tkinter widgets, we generally override the option_add() method. The properties and values passed to option_add() method will reflect the changes in all the widgets in the application. Thus, changing the default font will affect the font for all the widgets defined in the application.

Example

Here we will pass two parameters into the option_add() method, i.e., option_add("*font", "font-family font-size font-style font-orientation").

#Import the required libraries
from tkinter import *

#Create an instance of tkinter frame
win= Tk()

#Set the geometry of frame
win.geometry("600x400")

#Change the default Font that will affect in all the widgets
win.option_add( "*font", "lucida 20 bold italic" )
win.resizable(False, False)

#Create a Label
Label(win, text="This is a New Line").pack()
Button(win, text="Button-1", width=10).pack()

win.mainloop()

Output

Running the above code will set the default font as "lucida 20 bold italic" for all the widgets that uses textual information.

Now, go back to the program, remove the following line, and run it again.

win.option_add( "*font", "lucida 20 bold italic" )

The text will now appear in the default font −

Updated on: 26-Mar-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements