Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
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.
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.
Python Tkinter create LabelFrame example:
LabelFrame
is a container widget that provides a border and a title for grouping other widgets.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()
How to use LabelFrame in Tkinter:
LabelFrame
widget in Tkinter is used to create a labeled frame that can contain other widgets.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()
Customizing LabelFrame appearance in Tkinter:
LabelFrame
by adjusting options such as bg
(background color), bd
(border width), etc.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()
Building a form using LabelFrame in Tkinter:
LabelFrame
is often used to organize and group form elements together, providing a clear structure.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()