In this article, we’ll learn more about the Tkinter font class. When it comes to design a Graphical User Interface that is fully functional and adaptive to every environment Tkinter is the best option. It is a built-in graphics library that comes with Python programming language. So, let us see about some features of this special library and make our GUI development go to the next level.
Recommended read: Tkinter tutorial Part 1 – Making a Tkinter Canvas
Introduction to Tkinter
Python has set many GUI development libraries out on the internet. Most of them are open source and easy to use. But when they are not much relevant for a Getting Started Guide to GUIs. In this sense, a beginner needs to invest a lot more time and resources to learn and implement them according to his knowledge. This is a list of some of those libraries:
- WxPython etc.
But Tkinter lies in a very different frame. The reason why it comes built-in is that the basic Python Interpreter and IDLE– a lightweight python IDE all were designed using this package. Other than this, it is very easy to learn it from basics to advance than other libraries. Here is a list of the main components and classes of Tkinter.
- Root/Window: It is the main widget of this library. A resizable window that holds other sub widgets.
- Label: A label that can handle task of maintaining the labelling of each widget. It defines their properties.
- Button: A simple button that functions according to the user commands.
- Font: A special class which helps the creator to set the font of any sub-widget.
- Mainloop: This is the main loop that continuously runs the GUI infinite amount of times till the user do not exits.
A small Hello World code with Tkinter
In this section, we will write a code that creates a window of Hello World.
from tkinter import * root = Tk() root.title("Hello World") root.geometry("400x400")
This is a window that we created using just a few lines of code. Let us understand each line.
- Import the module.
- Then creating an instance of the Tk() class using the root variable. We can give any name to the object of the Tk() class.
- Setting the title of the window using the title() function. This renders the text on the top left corner of the window.
- Then the geometry() function helps to set the width and height of our window.
Understanding the Basics of Printing Text in Tkinter
The reason to take from basics is we need to understand that fonts in Tkinter are not limited to a specific class but they reflect in some other main widgets also.
Also read: Tkinter Frame and Label: An easy reference
Now that we have come into flow let us learn more things about it. Most importantly the GUI we design should look attractive and informative. So, to make it this way we have the Label() widget from Tkinter. What it does is it makes some text render on the screen that gives some info about anything we want. The syntax for it is very simple:
label_1 = Label(active_window, foreground_color, background_color, font, height, width)
There are a lot more parameters for the Label widget but, these much are alos perfect for the study. So this is the code for the same.
from tkinter import * root = Tk() root.title("Hello World") root.geometry("400x400") my_label = Label(root, text = "Hello World", fg = "Green", bg = "red", font = ("Arial", 32)) # setting up the labels my_label.pack() root.mainloop()
The main point to note down is that font acts as a parameter here. We can either give a tuple consisting of the Font type and size or directly the name. Various names are available for this parameter. A list of some is below.
- Times New Roman
- Bold italic
The pack() method is for displaying the label we created on the screen.
Working with the Tkinter Font Class
Fonts are some of the awesome writing styles that get created naturally. People across the world have different handwriting styles and those who know how to convert the same to a digital format, develop fonts that we use and love.
With the Tkinter font class, you can convert the text that is printed on a tkinter window to Bold, italic, Roman, and even change the font family.
Now that we have manipulated some of the font functionalities let us go through the main font class derived from the tkinter.font module. This module provides four main types:
To know more about this theory read the official documentation from this link.
We will directly see the application in the form of code.
from tkinter import * from tkinter.font import Font root = Tk() root.title("My Interface") root.geometry('400x400') my_font = Font( family = 'Times', size = 30, weight = 'bold', slant = 'roman', underline = 1, overstrike = 0 ) my_button = Button(root, text = 'Button', font = my_font) my_button.pack(pady = 20) root.mainloop()
Explanation of the above code:
- First we import all the sub-modules the tkinter module. Then from the tkinter.font module import Font class. This is the main utility class.
- Then create an Instance namely root. Set the title to “My interface”
- Set the geometry to 500×500 (width x height).
- Then create the my_font as an instance of Font class. It has an extensice set of parameters: family:- for selecting the type of font, size:- size of the font, weight:- font thickness bold/normal, slant:- slanting style roman/italic, underline:- to draw an underline under the text, overstrike:- draw a strike through line.
- After that a button widget that only gets a click and nothing. There we set the parameters as default window as root, text label as “Button”, font = my_font. This calls the created my_font object of the Font class.
- The mainloop runs the whole code and keeps track of every GUI event. It exits when use presses the close button.
In this way, we end this topic of knowing about the tkinter’s font class. This is a vary easy topic to understand and implement. Hope this will make the GUI knowledge more improved.