python Label

The Label widget in Tkinter is used to display text or images. It provides a way to show static content such as instructions, titles, or icons in a GUI application.

Syntax:

w = Label(master, options)

Here, master is the parent widget (such as a window or frame), and options are used to configure the appearance and behavior of the Label widget.

Options for Tkinter Label:

SN Option Description
1 anchor Specifies the position of the text within the widget. Default is CENTER. Possible values: N, E, S, W, NE, SW, etc.
2 bg Background color of the Label widget.
3 bitmap Displays a bitmap (graphical object) instead of text.
4 bd Border width of the Label in pixels. Default is 2 pixels.
5 cursor Changes the mouse pointer type when over the Label (e.g., arrow, dot).
6 font Font type used for the text displayed in the Label.
7 fg Foreground color (text color) of the Label.
8 height Height of the Label widget in pixels.
9 image Image to be displayed as the label.
10 justify Text alignment within the Label when multiple lines are present. Values: LEFT, RIGHT, CENTER.
11 padx Horizontal padding around the text. Default is 1 pixel.
12 pady Vertical padding around the text. Default is 1 pixel.
13 relief Type of border around the Label. Values: FLAT, RAISED, SUNKEN, GROOVE, RIDGE. Default is FLAT.
14 text The text to be displayed in the Label.
15 textvariable Associates a StringVar variable with the Label. Allows dynamic updates of the text displayed.
16 underline Displays a line under the specified letter in the text. Set this to the index of the letter to be underlined.
17 width Width of the Label in characters.
18 wraplength Specifies the maximum line length (in pixels) before wrapping text to the next line.

from tkinter import *

# Create the main window
root = Tk()
root.title("Tkinter Label Example")

# Create a Label with text
text_label = Label(root, text="Welcome to Tkinter!", font=("Helvetica", 16), fg="blue", padx=10, pady=10)
text_label.pack(pady=10)

# Create a Label with an image
# Load an image file (ensure you have an image named 'example.png' in your working directory)
image = PhotoImage(file="example.png")
image_label = Label(root, image=image, bg="lightgrey", relief="ridge")
image_label.pack(pady=10)

# Create a Label with multiple lines of text
multi_line_label = Label(root, text="This is a label with multiple lines.\nYou can use wraplength to control line wrapping.", wraplength=150, bg="lightyellow")
multi_line_label.pack(pady=10)

# Run the application
root.mainloop()

Explanation of the Example:

Text Label: Displays a text message with customized font, foreground color, and padding.

Image Label: Displays an image. Ensure the image file (example.png) is in the working directory.

Multi-line Label: Displays text that wraps within the specified width (wraplength). This allows for more readable content when dealing with longer text.

The Label widget is versatile and useful for displaying static content in your Tkinter applications.