Updated May 31, 2023
Introduction to Tkinter Font
In Python, a Tkinter font is a widget for styling or displaying text with various styles, sizes, and typeface variations. It includes options for normal, italic, and bold forms of the font. The Tkinter font widget encompasses properties such as size, weight, style, and slope for each character in the displayed or printed text.
How does Tkinter Font work in Python?
Tkinter, a GUI library in Python, provides various widgets for designing web pages. Among these widgets is the font widget, which allows for applying different font styles to texts, buttons, headings, and other elements. In this article, we will see how to define this font property in Tkinter and see all the properties for the font.
Constructor
Font constructor that provides different styling options is as below:
Font thickness and font slant are part of the weight and slant styling options in the font constructor. The font thickness can be specified using values such as “normal” for the default thickness and “bold” for bold text.
- Font family: This is also supported for defining the different font styles such as “Times New Roman,” Courier,” Helvetica, etc. are some of the font families the developers widely use.
- Font size: This is another option for defining the size of the text or character for web page styling, and the size in these fonts is measured in integer points.
- Font underlines property: This is used for underlying the text or font for selected text, and it uses 0 and 1 values for specifying this property such as 0 is the default value where there is no underline for the font, and if we specify 1, then it shows the text will be underlined.
- Font strikeout options: This is one of the different options of the font where it will allow us to overstrike the selected text.
Python Tkinter has a separate module for defining the font class using the tkFont module. This module offers a convenient way to utilize the font class, which includes constructors defined as options or arguments within the font class. To use this module, the first step is to import it. Now let us see the syntax of how to declare font class using the tkFont module.
import tkFont
Now we can declare font class by assigning it to the variable, and the syntax is as shown below:
f = tkFont.Font(arguments)
The arguments, in this case, can be the same font constructor options defined above. The font class of the tkFont module implements various font methods. For instance, the cget() method retrieves font attributes, while the config(**arguments or options) method allows customization of font attributes. The copy() method creates new instances of the current font, and the families() method retrieves different font families. Additionally, the names() method provides the defined font names. In the following section, let’s explore a few examples of using the Tkinter font.
Example
Now first, let us a simple example of how font class can be used on button or label names which is provided by the tkFont module.
from Tkinter import *
import tkMessageBox
import Tkinter as t
import tkFont as f
master = t.Tk()
master.geometry("500x200")
def func():
tkMessageBox.showinfo( "Hello Educba", "Button having Lucida with bold font and size is 20")
bfont = f.Font(family='Lucida Console', weight = 'bold', size = 20)
b1 = Button(master, text='Click me for Lucida', background='Cyan', fg='#000000', command = func)
b1['font'] = bfont
b1.pack()
def func1():
tkMessageBox.showinfo( "Hello Educba", "Button having Courier with Italic font and size is 10")
bfont1 = f.Font(family='Courier', slant = 'italic', size = 10)
b2 = Button(master, text='Click me for Courier ', background='Orange', fg='#07F', command = func1)
b2['font'] = bfont1
b2.pack()
master.mainloop()
Output:
In the above program, we have written a Python program using Tkinter and tkFont module for designing web pages having different font styling using the tkFont module. In the above program, when we click the button, a message box will pop up, but that has the default font settings as we have not customized.
Conclusion
In this article, we saw various font constructor options, which are the same as the arguments for the tkFont module, which provides various font classes for styling the font of the text. In this article, we saw an example of demonstrating this font property of Tkinter.
Recommended Articles
This is a guide to Tkinter Font. Here we discuss the definition and working of Tkinter Font along with examples and code implementation. You may also have a look at the following articles to learn more –