Tkinter Label – Underline Text

Tkinter Label underline text

To underline the text in a Label widget in Tkinter, create a Font object with the underline parameter set to True, and pass this font object as argument to the font parameter of Label() constructor.

Tkinter Label underline text

In this tutorial, you will learn how to underline the text a Label widget, with examples.

Steps to underline text in Label in Tkinter

Step 1

Import font from tkinter library, and create a Font class object with underline parameter set to True.

from tkinter import font
label_font = font.Font(underline=True)

Step 2

Create a label widget using tkinter.Label class constructor and pass the label_font (created in the above step) as argument to the font parameter.

tk.Label(window, text="Hello World!", font=label_font)

Examples

1. Label with underlined text

In this example, we shall create a Label with text Hello World!, underline the text using Font class, and display it in the main window.

Program

import tkinter as tk
from tkinter import font

# Create the main window
window = tk.Tk()
window.title("PythonExamples.org")
window.geometry("300x200")

# Create font object
label_font = font.Font(underline=True)

# Create a label widget with specific font
label = tk.Label(window, text="Hello World!", font=label_font)

# Pack the label widget to display it
label.pack()

# Run the application
window.mainloop()

Output

Tkinter Label – Underline Text

Run on a MacOS.

Tkinter Label underline text - example

Summary

In this Python Tkinter tutorial, we learned how to underline the text in a Label widget, with examples.

Related Tutorials

Code copied to clipboard successfully 👍