Underline Text In Tkinter Label Widget

Tkinter is a module in Python which is used to create GUI applications. It has many useful widgets such as Label, Button, Radiobutton, Checkbutton, Listbox, and more. In this article, we will learn about changing the color of a Tkinter label widget in Python.

By default, the text of a Label in Tkinter is not underlined. To have an underline in a text of Label, use the ‘font’ option of Label, and mention an attribute “underline” in it. This makes the text of a Label in Tkinter underlined.

Syntax to underline Text in Label

label_name = tkinter.Label(parent,text="text_to_display",font=("font_family_name",font_size,"underline")

Let us understand this with an example code below.

Underlining Text in a Tkinter Label

To underline the text in a Tkinter Label, you need to modify the font of the Label. Tkinter uses the font attribute to control the font style, size, and other properties. We can use the font.Font class to create a font object with an underline attribute.

Importing Necessary Modules:

import tkinter as tk
from tkinter import font

Besides importing Tkinter, we also import the font module from Tkinter to create and modify font properties.

Creating a Tkinter Window:

We create the main Tkinter window and set its title.

Python
root = tk.Tk()
root.title("Underline Text in Tkinter Label")

Creating an Underlined Font:

Here, we create a Font object with the underline attribute set to True. You can customize the family and size attributes to suit your needs.

Python
underlined_font = font.Font(family="Helvetica", size=12, underline=True)

Applying the Underlined Font to a Label:

We create a Label widget and apply the underlined font to it. The text in the Label will now appear underlined.

Python
label = tk.Label(root, text="This text is underlined", font=underlined_font)
label.pack(pady=20)

Running the Application

This starts the Tkinter event loop, displaying the window and its contents.

Python
root.mainloop()

Complete Code

Here’s the complete code for a Tkinter application that displays an underlined text Label:

Python
import tkinter as tk
from tkinter import font

# Create the main window
root = tk.Tk()
root.title("Underline Text in Tkinter Label")

# Create an underlined font
underlined_font = font.Font(family="Helvetica", size=12, underline=True)

# Create a Label widget with underlined text
label = tk.Label(root, text="This text is underlined", font=underlined_font)
label.pack(pady=20)

# Run the application
root.mainloop()

Output

Example 2:

This code creates a simple graphical user interface (GUI) using the Tkinter library in Python. The application window is titled “w3wiki” and has a size of 280×100 pixels with a green background.

Python
# Importing tkinter
import tkinter as tk

# GUI
root = tk.Tk()
root.title("w3wiki")
root.geometry("280x100")
root.config(bg="green")

# Label with text underlined
tk.Label(root, text="Welcome to GFG!", fg="white", bg="green",
         font=("Arial", 16, "underline")).pack(pady=15)
root.mainloop()

Output:

Output with underline text in Label


Conclusion:

In this article, we have discussed how to make the text of a Label in Tkinter underline easily.



Contact Us