Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Create LabelFrame and add widgets to it in Tkinter

The LabelFrame in tkinter is used to create a frame for a group of widgets with an optional title. It acts like a normal frame but can have a label, making it particularly useful for grouping related widgets.

Creating a LabelFrame and Adding Widgets in tkinter:

1. Import Required Libraries:

import tkinter as tk

2. Initialize Main Application Window:

root = tk.Tk()
root.title("LabelFrame Tutorial")

3. Create a LabelFrame:

Let's create a LabelFrame with the title "User Details".

user_frame = tk.LabelFrame(root, text="User Details", padx=10, pady=10)
user_frame.pack(padx=10, pady=10)

Here, padx and pady within the LabelFrame are used to create some padding around the widgets inside the frame.

4. Add Widgets to the LabelFrame:

We can now add widgets to the LabelFrame just like we would with a regular frame.

# Adding a Label and Entry for username
tk.Label(user_frame, text="Username:").grid(row=0, column=0, sticky="w", padx=5, pady=5)
username_entry = tk.Entry(user_frame)
username_entry.grid(row=0, column=1, padx=5, pady=5)

# Adding a Label and Entry for password
tk.Label(user_frame, text="Password:").grid(row=1, column=0, sticky="w", padx=5, pady=5)
password_entry = tk.Entry(user_frame, show="*")
password_entry.grid(row=1, column=1, padx=5, pady=5)

# Adding a submit button
submit_button = tk.Button(user_frame, text="Submit")
submit_button.grid(row=2, column=0, columnspan=2, pady=10)

Note that we're using the grid geometry manager inside the LabelFrame to place the widgets.

5. Mainloop to Run the Application:

root.mainloop()

Complete Code:

import tkinter as tk

root = tk.Tk()
root.title("LabelFrame Tutorial")

user_frame = tk.LabelFrame(root, text="User Details", padx=10, pady=10)
user_frame.pack(padx=10, pady=10)

tk.Label(user_frame, text="Username:").grid(row=0, column=0, sticky="w", padx=5, pady=5)
username_entry = tk.Entry(user_frame)
username_entry.grid(row=0, column=1, padx=5, pady=5)

tk.Label(user_frame, text="Password:").grid(row=1, column=0, sticky="w", padx=5, pady=5)
password_entry = tk.Entry(user_frame, show="*")
password_entry.grid(row=1, column=1, padx=5, pady=5)

submit_button = tk.Button(user_frame, text="Submit")
submit_button.grid(row=2, column=0, columnspan=2, pady=10)

root.mainloop()

When you run the provided code, you'll get a window with a LabelFrame titled "User Details", containing labels, entries for username and password, and a submit button.

  1. Python Tkinter create LabelFrame example:

    • Description: Tkinter's LabelFrame is a container widget that provides a border and a title for grouping other widgets.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      # Create a LabelFrame
      label_frame = tk.LabelFrame(root, text="Label Frame")
      label_frame.pack(padx=10, pady=10)
      
      root.mainloop()
      
  2. How to use LabelFrame in Tkinter:

    • Description: The LabelFrame widget in Tkinter is used to create a labeled frame that can contain other widgets.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      # Create a LabelFrame
      label_frame = tk.LabelFrame(root, text="Label Frame")
      
      # Add widgets to the LabelFrame
      label = tk.Label(label_frame, text="This is a LabelFrame")
      button = tk.Button(label_frame, text="Click Me")
      
      label.pack()
      button.pack()
      
      label_frame.pack(padx=10, pady=10)
      
      root.mainloop()
      
  3. Customizing LabelFrame appearance in Tkinter:

    • Description: You can customize the appearance of a LabelFrame by adjusting options such as bg (background color), bd (border width), etc.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      # Create a custom-styled LabelFrame
      label_frame = tk.LabelFrame(root, text="Styled Label Frame", bg="lightgray", bd=2, relief=tk.GROOVE)
      label_frame.pack(padx=10, pady=10)
      
      root.mainloop()
      
  4. Building a form using LabelFrame in Tkinter:

    • Description: LabelFrame is often used to organize and group form elements together, providing a clear structure.
    • Code:
      import tkinter as tk
      
      def submit_form():
          username = entry_username.get()
          password = entry_password.get()
          print("Username:", username)
          print("Password:", password)
      
      root = tk.Tk()
      
      # Create a form using LabelFrame
      form_frame = tk.LabelFrame(root, text="Login Form")
      form_frame.pack(padx=10, pady=10)
      
      # Add entry widgets to the form
      label_username = tk.Label(form_frame, text="Username:")
      label_password = tk.Label(form_frame, text="Password:")
      entry_username = tk.Entry(form_frame)
      entry_password = tk.Entry(form_frame, show="*")  # Password entry with hidden characters
      
      label_username.grid(row=0, column=0, sticky=tk.W, pady=5)
      label_password.grid(row=1, column=0, sticky=tk.W, pady=5)
      entry_username.grid(row=0, column=1, pady=5)
      entry_password.grid(row=1, column=1, pady=5)
      
      # Add a button to submit the form
      submit_button = tk.Button(form_frame, text="Submit", command=submit_form)
      submit_button.grid(row=2, columnspan=2, pady=10)
      
      root.mainloop()