How to Set Border of Tkinter Label Widget


In Python, Tkinter is GUI(Graphical User Interface) based library that accesses easier and faster ways to build the GUI application. The Tkinter Label Widget will be used to build the border that helps to improve the visibility of the user interface. A Tkinter Label Widget is such type of area that shows the text or images. In Python, we have some built−in functions such as Label(), TK(), Frame(), pack(), and, config() will be used to Set the Border of Tkinter Label Widget.

Syntax

The following syntax is used in the examples-

Tk()

The Tk() is a built−in function in Python that manages the main window along with all the components of tkinter application.

label()

The built−in function label() follows the tkinter module to show the boxes where users can put some text or images.

pack()

The pack() is an in−built function in Python that creates the widgets in horizontal and vertical boxes.

mainloop()

The mainloop() is a Tk class built−in method that initiates the main event loop of a tkinter application. It's an infinite loop that watches for events and processes them until the window closes.

Frame()

The Frame() is an in−built function in Python that is responsible for managing the widgets.

config()

The built−in function config() state the configuration of an object attribute. The configuration always depends on available parameters.

Example 1: Using the bd attribute

In the following example, we will start the program importing module named tkinter and take the object reference as tk. Then use the built−in function Tk() that manages the root of this window. Next, use the built−in function Label() that accepts some parameters− root, text(to fill the text inside the border), bd(set the border value), relief(set the style of the border) and store it in the variable label. Now use the pack() which the widgets according to the text. Finally, using mainloop() it will display the result.

Example

import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Label with Border", bd=10, relief="solid")
label.pack()
root.mainloop()

Output

Example 2: Using a Frame widget

In the following example, the program uses Frame() which accepts some parameters to define the container for other widgets. Next, using pack() it add the frame to the window. Moving ahead to use the built−in function Label() that set the frame along its text. Using pack() and mainloop() it displays the output.

Example

import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, borderwidth=2, relief="solid")
frame.pack()
label = tk.Label(frame, text="Label with Border")
label.pack()
root.mainloop()

Output

Example 3: Using a Frame widget

In the following example, we will use the built−in method config() that configure the border of Tkinter by setting border color to red and thickness to value 2.

Example

import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Label with Border")
label.config(highlightbackground="red", highlightthickness=2)
label.pack()
root.mainloop()

Output

Conclusion

The Widgets are simple dialogue boxes or buttons used in different operations. The program uses various built−in functions such as config(), Frame(), Label(), etc to build the boxes in all the above examples. The Tkinter Label Widget is a GUI−based Python program that helps build applications related to different operating systems such as Linux, macOS, and Windows.

Updated on: 14-Aug-2023

678 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements