How to use a custom font in Tkinter?

TkinterServer Side ProgrammingProgramming

To define and display a custom font in Python tkinter, we generally use an inbuilt font library defined in tkinter. In order to import the tkinter Font library in the notebook, type the following in the shell,

from tkinter.font import Font

Now, create an Object of Font using the Font(..options) function and define other properties of the font such as font-family, size, weight, slant, underline, strike, etc.

Example

#Import the required library
from tkinter import *
#Create an instance of tkinter frame
win = Tk()
win.geometry("750x250")
#Create a String Object and set the default value
var = StringVar()
#Create a text label
label = Label(win, textvariable = var, font=('Consolas 20 bold'))
label.pack()
#Create an entry widget to change the variable value
text = Entry(win, textvariable = var)
text.pack()
win.mainloop()

Output

Running the above code will display a window with an Entry widget and a Label. The Label gets updated whenever we enter some keywords in the Text Field.

raja
Published on 16-Apr-2021 07:04:06
Advertisements