Python 3 – Tkinter LabelFrame
Tkinter is a standard GUI (Graphical User Interface) package that comes installed with Python. It is easy to use and allows users to create interactive and graphical applications. Tkinter has several widgets that can be used to create applications, and LabelFrame is one such widget. In this article, we will investigate the LabelFrame widget in detail.
Overview
The LabelFrame widget is used to group together other widgets into a logical section. It is similar to a frame, but with a label that is displayed on the top. The label can be used to define the purpose of the grouped widgets.
Syntax
The syntax for creating a LabelFrame widget is:
labelFrame = LabelFrame(parent, options)
Where parent
is the widget to which the label frame will be added, and options
are the options that can be used to configure the label frame.
Here is a simple example that demonstrates how to create and add widgets to a LabelFrame:
from tkinter import *
root = Tk()
root.geometry("200x150")
my_label_frame = LabelFrame(root, text="My Label Frame", padx=5, pady=5)
my_label_frame.pack(fill="both", expand="yes")
my_label = Label(my_label_frame, text="Hello, World!")
my_label.pack()
root.mainloop()
In the above example, we create a LabelFrame
widget named my_label_frame
and set its text to “My Label Frame”. We also add padding to the widget using the padx
and pady
options. We then add this LabelFrame
widget to the root
widget using the pack
method.
Inside the LabelFrame
widget, we create a Label
widget named my_label
and set its text to “Hello, World!”. We then add this Label
widget to the my_label_frame
widget using the pack
method.
Options
The following options can be used to configure a LabelFrame widget:
bg
– sets the background color of the widget.bd
– sets the border width of the widget.fg
– sets the foreground (text) color of the widget.font
– sets the font used for the text in the widget.height
– sets the height of the widget in pixels.labelanchor
– sets the anchor for the label, which can be “n”, “s”, “w”, “e”, “nw”, “ne”, “sw”, or “se”. Defaults to “nw”.pady
– sets the padding on the top and bottom of the widget in pixels.padx
– sets the padding on the left and right sides of the widget in pixels.relief
– sets the relief style of the widget, which can be “flat”, “raised”, “sunken”, “groove”, or “ridge”. Defaults to “raised”.text
– sets the text displayed on the label of the widget.width
– sets the width of the widget in pixels.
Methods
The following methods can be used with a LabelFrame widget:
configure(**options)
– configures one or more of the widget’s options.pack(**options)
– packs the widget into its parent widget.place(**options)
– places the widget at a specific position in its parent widget.grid(**options)
– organizes the widget in a grid layout.
Example
Here is a more advanced example that demonstrates how to use the LabelFrame widget to create a form with multiple widgets:
from tkinter import *
root = Tk()
root.geometry("300x200")
personal_info_frame = LabelFrame(root, text="Personal Information", padx=5, pady=5)
personal_info_frame.pack(fill="both", expand="yes")
first_name_label = Label(personal_info_frame, text="First Name:")
first_name_label.grid(row=0, column=0, sticky="e")
first_name_entry = Entry(personal_info_frame)
first_name_entry.grid(row=0, column=1)
last_name_label = Label(personal_info_frame, text="Last Name:")
last_name_label.grid(row=1, column=0, sticky="e")
last_name_entry = Entry(personal_info_frame)
last_name_entry.grid(row=1, column=1)
address_label = Label(personal_info_frame, text="Address:")
address_label.grid(row=2, column=0, sticky="ne")
address_entry = Text(personal_info_frame, width=20, height=3)
address_entry.grid(row=2, column=1)
gender_label = Label(personal_info_frame, text="Gender:")
gender_label.grid(row=3, column=0, sticky="e")
gender_var = StringVar()
gender_var.set("Male")
male_radio_button = Radiobutton(personal_info_frame, text="Male", variable=gender_var, value="Male")
male_radio_button.grid(row=3, column=1, sticky="w")
female_radio_button = Radiobutton(personal_info_frame, text="Female", variable=gender_var, value="Female")
female_radio_button.grid(row=4, column=1, sticky="w")
root.mainloop()
In this example, we create a LabelFrame
widget named personal_info_frame
and set its text to “Personal Information”. We also add padding to the widget using the padx
and pady
options. We then add this LabelFrame
widget to the root
widget using the pack
method.
Inside the personal_info_frame
widget, we create several other widgets, including Label
widgets for the first name, last name, and gender fields, Entry
widgets for the first name and last name fields, a Text
widget for the address field, and Radiobutton
widgets for the gender field.
We then use the grid
method to position each widget in a 2-column grid layout.
Conclusion
LabelFrame is a useful widget in Tkinter that allows users to group together other widgets into a logical section. It is similar to a frame, but with a label that is displayed on the top. Users can easily configure the LabelFrame widget using various options and add other widgets inside it to create more complex UI elements. It is a great way to organize the UI elements and make it more readable and user-friendly.