Changing the Default Font for all the widgets in Tkinter

TkinterPythonGUI-Programming

Let us consider a case where we want to change the default font of a Tkinter application. To apply the font and setting it as the default font for a particular application, we have to use option_add(**options) method where we specify a property such as background color, font, etc. The changes made after defining the method will force all the widgets to inherit the same property.

Example

In the given script, we have set a default font for the application such that it can be used for all the widgets defined in the application.

#Import the required libraries
from tkinter import *

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

win.geometry("700x350")
#Add fonts for all the widgets
win.option_add("*Font", "aerial")

#Set the font for the Label widget
win.option_add("*Label.Font", "aerial 18 bold")

# Define the backround color for all the idgets
win.option_add("*Background", "bisque")

#Display bunch of widgets
Label(win, text="Label").pack()
Button(win, text="Button").pack()

#Create a Listbox widget
w = Listbox(win)
for i in range(5):
   w.insert(i, "item %d" % (i+1))
w.pack()

w = Text(win, width=20, height=10)
w.insert(1.0, "a text widget")
w.pack()

win.mainloop()

Output

Running the above code will display a window with a Label widget, a Button, a Listbox, and a Text Widget. In the given output, all widgets inherit the same properties.

raja
Published on 25-May-2021 09:13:32
Advertisements