Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Label Widget in Tkinter

The Label widget in tkinter is used to display text or an image. It's one of the most commonly used widgets in tkinter applications, especially when you want to present static information to the user.

Label Widget in tkinter Tutorial:

1. Import Required Libraries:

import tkinter as tk

2. Initialize Main Application Window:

root = tk.Tk()
root.title("Label Widget Tutorial")

3. Creating a Basic Label:

label = tk.Label(root, text="Hello, Tkinter!")
label.pack(padx=20, pady=20)

4. Some Commonly Used Options with Label:

  • text: The text to be displayed.
  • bg: Background color.
  • fg: Foreground color (color of the text).
  • font: Defines the font type and size.
  • image: Used to display an image.
  • anchor: Alignment of the text or image inside the widget. Values can be N, NE, E, SE, S, SW, W, NW, or CENTER.
  • relief: Defines the type of the border. Some values are tk.FLAT (default), tk.RAISED, tk.SUNKEN, tk.GROOVE, and tk.RIDGE.
  • padx and pady: External padding around the text or image.

Example:

label_custom = tk.Label(root, text="Custom Label", bg="yellow", fg="red", font=('Arial', 14, 'bold'), relief=tk.RAISED, padx=10, pady=10)
label_custom.pack(pady=20)

5. Displaying an Image with Label:

To display an image in a label, use the Image module from tkinter.

img = tk.PhotoImage(file="path_to_image.png")  # Use the appropriate path to your image
img_label = tk.Label(root, image=img)
img_label.pack(pady=20)

6. Combining Text and Image:

Using compound option, you can have both text and image in a label. The compound option specifies the position of the image relative to the text, e.g., tk.LEFT, tk.TOP, etc.

combo_label = tk.Label(root, text="Text and Image", image=img, compound=tk.LEFT)
combo_label.pack(pady=20)

7. Mainloop to Run the Application:

root.mainloop()

Complete Code (excluding image-related code):

import tkinter as tk

root = tk.Tk()
root.title("Label Widget Tutorial")

label = tk.Label(root, text="Hello, Tkinter!")
label.pack(padx=20, pady=20)

label_custom = tk.Label(root, text="Custom Label", bg="yellow", fg="red", font=('Arial', 14, 'bold'), relief=tk.RAISED, padx=10, pady=10)
label_custom.pack(pady=20)

root.mainloop()

When you run the code, you'll see a window with two labels, showcasing the default look and the customized look of the Label widget. Adjust the properties as needed to get the desired appearance for your labels.

  1. Python Tkinter create Label example:

    • Description: The Label widget in Tkinter is used to display text or images.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      # Create a Label
      label = tk.Label(root, text="Hello, Tkinter!")
      label.pack(padx=10, pady=10)
      
      root.mainloop()
      
  2. How to use Label widget in Tkinter:

    • Description: The Label widget is used to display text or images in a Tkinter GUI.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      # Create a Label
      label = tk.Label(root, text="Hello, Tkinter!")
      label.pack(padx=10, pady=10)
      
      root.mainloop()
      
  3. Displaying text and images with Label in Tkinter:

    • Description: The Label widget can display both text and images. You can set the text or image option accordingly.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      # Display text and image using Label
      label_text = tk.Label(root, text="Hello, Tkinter!")
      label_image = tk.Label(root, image=tk.PhotoImage(file="image.png"))
      
      label_text.pack(pady=10)
      label_image.pack(pady=10)
      
      root.mainloop()
      
  4. Tkinter Label widget options and configuration:

    • Description: The Label widget has various options for configuring its appearance, including options for font, background color, foreground color, etc.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      # Customize the appearance of a Label
      label = tk.Label(root, text="Styled Label", font=("Arial", 12), bg="lightgray", fg="blue")
      label.pack(padx=10, pady=10)
      
      root.mainloop()
      
  5. Handling events with Label widget in Tkinter:

    • Description: The Label widget can respond to events, such as mouse clicks, by binding event handlers to it.
    • Code:
      import tkinter as tk
      
      def on_label_click(event):
          label.config(text="Clicked!")
      
      root = tk.Tk()
      
      # Create a Label with an event handler
      label = tk.Label(root, text="Click Me!")
      label.pack(padx=10, pady=10)
      
      label.bind("<Button-1>", on_label_click)
      
      root.mainloop()
      
  6. Building layouts with Label in Tkinter:

    • Description: The Label widget is commonly used to display information and contribute to the layout of Tkinter GUIs.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      # Build a layout using Labels
      label1 = tk.Label(root, text="Label 1")
      label2 = tk.Label(root, text="Label 2")
      label3 = tk.Label(root, text="Label 3")
      
      label1.pack()
      label2.pack()
      label3.pack()
      
      root.mainloop()
      
  7. Tkinter Label widget grid and pack methods:

    • Description: The Label widget can be placed in the Tkinter window using either the pack or grid method for layout management.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      # Use both pack and grid to manage Labels
      label1 = tk.Label(root, text="Label 1")
      label2 = tk.Label(root, text="Label 2")
      
      label1.pack(side=tk.LEFT)
      label2.grid(row=0, column=1)
      
      root.mainloop()