LAST UPDATED: AUGUST 24, 2020
Python Tkinter LabelFrame Widget
In this tutorial, we will cover the Tkinter LabelFrame widget in Python with its syntax and few examples. The LabelFrame widget is mainly used to draw borders around the child widgets.
-
This widget is a bordered container widget and is used to group the related widgets in a Tkinter application to provide a better user experience to the user.
-
For example, we can group the radiobutton widgets used in an application using the labelframe widget.
-
One can also add a title for the LabelFrame widget(we will see this in the code example).
-
The LabelFrame widget is simply a variant of the Frame widget and it has all the features of a frame.
Note: If you have used HTML for web development, then the labelframe is just like HTML fieldset tag.
Tkinter LabelFrame Widget
The syntax of the LabelFrame widget is given below. Let us see:
w = LabelFrame(master, option=value)
In the above syntax, the master
parameter denotes the parent window. You can use many options
to configure the labelframe and these options are written as comma-separated key-value pairs.
Tkinter LabelFrame Widget Options:
Following are the various options used with LabelFrame widgets:
Name of the Option |
Description |
height |
This option is used to represent the height of the widget. |
width |
This option is used to represent the width of the frame. |
text |
This option represents the string containing the text of the Label. |
relief |
This option represents the style of the border.The default value of this option is GROOVE |
padx |
This option represents the horizontal padding of the widget |
pady |
This option represents the vertical padding of the widget |
font |
This option represents the font type of the text of the widget |
highlighthickness |
This option represents the width of the focus highlight border |
highlightbackground |
This option indicates the color of the focus highlight border at the time when the widget doesn't have the focus |
highlightcolor |
This option indicates the color of the focus highlight when the widget is under the focus |
bg |
This option indicates the background color of the widget |
bd |
This option is used to represent the size of the border around the indicator.The default value of this option is 2 pixels. |
Class |
The default value of this option is LabelFrame. |
colormap |
This option is mainly used to specify which colomap to be used for this widget.With the help of this option, we can reuse the colormap of another window on this widget.The colormap means 256 colors that are used to form the graphics |
container |
The LabelFrame becomes the container widget if we will set the value of this option to true.The default value of this option is false |
cursor |
This option will convert the mouse pointer to the specified cursor type and it can be set to an arrow, dot, etc |
fg |
This option is used to indicate the foreground color of the widget |
labelAnchor |
This option represents the exact position of the text inside the widget. The default value of this option is NW(north-west) |
labelwidget |
This option indicates the widget to be used for the label. Also,the frame uses the text for the label if no value specified |
Tkinter LabelFrame Widget Example
Below we have a basic example of the LabelFrame widget. Let us see the code snippet given below:
from tkinter import *
win = Tk()
win.geometry("300x200")
labelframe1 = LabelFrame(win, text="Happy Thoughts!!!")
labelframe1.pack(fill="both", expand="yes")
toplabel = Label(labelframe1, text="You can put your happy thoughts here")
toplabel.pack()
labelframe2 = LabelFrame(win, text = "Changes You want!!")
labelframe2.pack(fill="both", expand = "yes")
bottomlabel = Label(labelframe2, text = "You can put here the changes you want,If any!")
bottomlabel.pack()
win.mainloop()
As you can see in the output above, we have created two labelframe widgets, in which we have added text for the labelframe widgets, and inside the labelframe widget we have one label widget. We can have as many widgets inside the labelframe widget, as we want.
Summary:
So with this we have completed the Tkinter labelframe widget which is just like HTML fieldset tag, if you know HTML. The labelframe widget is used to create a border around other widgets to group other widgets present in your application.