Creating and Customizing Labels in Tkinter With Ttkbootstrap GUI Library
by xanthium-enterprises in Circuits > Software
11 Views, 0 Favorites, 0 Comments
Creating and Customizing Labels in Tkinter With Ttkbootstrap GUI Library
 and Setting Fonts and Font sizes of Labels using Python)
In this Instructable , we’ll explore how to create and customize labels in Tkinter with the help of ttkbootstrap, an extension to the built-in ttk module. We will also dive into adjusting the font styles and sizes of those labels to make them look more modern and appealing. We have already covered the installation of the ttkbootstrap and creation of the basic tkinter window in the previous tutorial.
Supplies



- Python (version 3.6 or higher)
- ttkbootstrap library installed on your sytem
- An IDE like PyCharm or Thonny ,if you like .
All the Source codes can be downloaded from our Website.
Before we dive into creating labels, ensure that you have Tkinter and ttkbootstrap installed. You can install ttkbootstrap via pip if it’s not already installed
Creating a Simple Label to Display Text Using Tkinter

Now, let’s create the label. We will use the ttkbootstrap.Label() widget, which allows for theming. In the previous Instructable we have covered how to create a simple window using the ttkbootstrap library and set multiple themes.
Now we will first create a simple label without any themes .
Please type the following code into the IDE or text editor.
First, the ttkbootstrap library is imported .
The main application window is created using ttkbootstrap.Window(), which initializes the main Tkinter window .
The title of the window is set to "Labels in Tkinter (ttkbootstrap)" using the root.title() method, which will appear in the title bar of the window.
A label widget is then created using ttkbootstrap.Label(text='My Label'), which will display the text "My Label" inside the window.
The label is placed inside the window using the .pack() method, which adds it to the layout.
The .pack() method is a geometry manager used to organize and position widgets inside a container (like a window or Label as in our case). It is one of the three primary geometry managers in Tkinter, alongside .grid() and .place(). The .pack() method automatically arranges widgets in a vertical or horizontal direction within the parent container.
The pady=30 argument adds 30 pixels of vertical padding around the label to space it out from other potential elements or the edges of the window.
Finally, root.mainloop() starts the Tkinter event loop, which is essential for keeping the window open and allowing interaction with the user. This ensures that the window will remain active and display until the user closes it.
On running the Code you will get the above image.
Changing the Size and Font Type in Tkinter Label

In the above example we created a simple label with default font and size. Now lets say we want to change the size and shape of the tkinter labels font, In this section we will teach you how to create a Labels with various font types like Arial, Times New Roman etc.
To change the font, you need to specify it as a tuple. This tuple consists of three components:
- the font family,
- the font size,
- and an optional font style.
The font family refers to the typeface you want to use, such as "Times New Roman", "Verdana", or "Georgia".
The font size defines the size of the text, typically represented by a number (e.g., 12, 16, or 20).
Additionally, you can include a font style, such as "bold", "italic", or "underline", to further customize the text's appearance.
Here is the code to illustrate the Font properties of a tkinter label.
In these lines, four label widgets are created with different font styles:
- MyLabel1 uses the Times New Roman font with a size of 18 and the bold style.
- MyLabel2 uses the Times New Roman font with a size of 18 and the italic style.
- MyLabel3 uses the Comic Sans MS font with a size of 18, without any specific style (normal).
- MyLabel4 uses the Comic Sans MS font with a size of 18 and the bold style.
The font attribute specifies the font family, size, and optionally the style (like bold or italic) for each label.
Each label is added to the window using the .pack() method. This method automatically places the widget in the window in a vertical order, one after the other.