Introduction to Tkinter Font
In Python, Tkinter font is defined as a widget that is used for styling the text or displaying the text where there are many different styles, sizes, or any typeface variation which includes a display of text in the normal or italic or bold form of the font for the text. In general, we can define the Tkinter font as a widget having all the properties and the combination of typeface properties such as size, weight, style and slope of each character in the test displayed or can be printed also and the font can widget can be applied not only to characters or letters but also to numbers, symbols, special characters, punctuation marks, etc.
How Tkinter Font works in Python?
In this article, we are discussing the Tkinter font widget styling as to which is defined as styling the font of the text which is very commonly used for designing in the fonts of the texts in web pages. In Python, the Tkinter is a GUI library that is used for designing the web pages and it provides various widgets to design such web pages and one among this is a font that can be applied for texts, buttons, headings, etc. In this article, we will see how to define this font property in Tkinter and to see all the properties for font then we have to see the font constructor which is supported by the Tkinter for various styling options of the font.
Font constructor that provides different styling options are as below:
Font thickness and font slant which belong to weight and slant styling options in the font constructor for specifying the font thickness by using values such as normal for normal and is the default value, bold values are given, whereas for font slant includes normal and italic values having the normal as default value.
1. 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 which are widely used by the developers.
2. Font size: This is another option for defining the size of the text or character for web pages styling and the size in these fonts is measured in integer points and there is also another way to declare the font size using pixels which are done using negative values.
3. Font underlines property: which 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.
4. Font strikeout options: this is one of the different options of the font where it will allow us to overstrike the text that is selected. In this also it takes a Boolean value saying 1 as true then there is a line drawn over the text and if it is false then there is no line drawn over the text and 0 is the default value.
In Python Tkinter, there is a separated module for defining the font class using the tkFont module where this module provides us an easy way to use font class in which there are constructors of font defined as options or arguments to the font class and to use this module we need to first import this module. Now let us see the syntax of how to declare font class using the tkFont module.
First, we need to import tkFont and that is done as shown below:
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 here can be the same font constructor options defined above and the font methods are defined using the font class of tkFont module such as cget() for retrieving the attributes of font, config(**arguments or options) this method is used for customization of attributes of the font, copy()method is used for returning new instances of the present font, families() which returns a different type of font families, names() which returns the font names that are defined, etc. This Tkinter font can be used for changing the font of labels, buttons, etc. Now in the below section let us see few examples of Tkinter font.
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 Tkinter as t
import tkFont as f
master = t.Tk()
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
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
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. We have to first import both Tkinter and tkFont module and then define the parent window in which the widgets are being placed such as buttons in the above code. So we have given two buttons having the button name or text in different font constructor options such as font-family: Lucida, weight as “bold” for button1 with size as 20, slant as “italic”, font family as a courier which is used in button 2 and the text will be in italics with size 10. 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.
In this article, we conclude that the Tkinter in Python has different varieties of widgets followed by the properties for styling them and one such we discussed in this article is a font that is used for defining the text size, weight and look or style. In this article, we saw various font constructor options which are also the same for 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.