The LabelFrame widget in Tkinter is an extension of the Frame widget that provides a bordered container with a title or label. It’s useful for grouping related widgets together, often with a descriptive label.
w = LabelFrame(top, options)
• top: The parent widget or window where the LabelFrame will be placed.
• options: Various configuration options for the LabelFrame (listed below).
| SN | Option | Description |
|---|---|---|
| 1 | bg | Background color of the widget. |
| 2 | bd | Size of the border around the LabelFrame. Default is 2 pixels. |
| 3 | class_ | The class name of the widget, default is 'LabelFrame'. |
| 4 | colormap | Specifies the colormap to be used for the widget, allowing reuse of the colormap from another window. |
| 5 | container | If set to True, the LabelFrame acts as a container widget. Default is False. |
| 6 | cursor | Type of cursor to display when the mouse is over the widget (e.g., arrow, dot). |
| 7 | fg | Foreground color of the widget text. |
| 8 | font | Font type of the widget text. |
| 9 | height | Height of the LabelFrame widget. |
| 10 | labelanchor | Position of the label text within the widget. Default is NW (north-west). |
| 11 | labelwidget | Widget to use for the label. If not specified, the widget uses the text option for the label. |
| 12 | highlightbackground | Color of the focus highlight border when the widget doesn't have focus. |
| 13 | highlightcolor | Color of the focus highlight when the widget has focus. |
| 14 | highlightthickness | Width of the focus highlight border. |
| 15 | padx | Horizontal padding of the widget. |
| 16 | pady | Vertical padding of the widget. |
| 17 | relief | Style of the border. Default is GROOVE. |
| 18 | text | Text for the label of the LabelFrame. |
| 19 | width | Width of the LabelFrame widget. |
Here's a basic example of creating a LabelFrame with a label and adding widgets to it:
from tkinter import *
# Create the main window
root = Tk()
root.title("LabelFrame Example")
# Create a LabelFrame widget
label_frame = LabelFrame(root, text="Group 1", padx=10, pady=10, bg='lightgrey', relief=RAISED)
label_frame.pack(padx=20, pady=20, fill="both", expand=True)
# Create some widgets to add to the LabelFrame
label1 = Label(label_frame, text="This is a label")
label1.pack(padx=5, pady=5)
button1 = Button(label_frame, text="Click Me")
button1.pack(padx=5, pady=5)
# Run the Tkinter event loop
root.mainloop()
• text: The text that appears as the label for the LabelFrame.
• padx and pady: Add padding inside the LabelFrame around the child widgets.
• relief: Adds a border style to the LabelFrame. Here, RAISED creates a 3D effect.
• bg: Sets the background color of the LabelFrame.
The example creates a LabelFrame titled "Group 1" and adds a label and button to it. Padding is added around the LabelFrame and its children to improve layout appearance.