Home » Python Tkinter LabelFrame

Python Tkinter LabelFrame

Java SE 11 Developer (Upgrade) [1Z0-817]
1 Year Subscription
Oracle Java Certification
Java SE 11 Programmer I [1Z0-815] Practice Tests
Spring Framework Basics Video Course
Java SE 11 Programmer II [1Z0-816] Practice Tests

In this article we look at the LabelFrame widget

The LabelFrame widget is used to draw a border around its child widgets – so you can think of this as a way of grouping a set of widgets together

Table of Contents

Syntax

mylabel = LabelFrame(top, options)

A list of options is given below.

Option Description
bg The background color of the widget.
bd It represents the size of the border shown around the indicator. The default is 2 pixels.
Class The default value of the class is LabelFrame.
colormap This option is used to specify which colormap is to be used for this widget.
container If this is set to true, the LabelFrame becomes the container widget. The default value is false.
cursor It can be set to a cursor type, i.e. arrow, dot, etc. the mouse pointer is changed to the cursor type when it is over the widget.
fg It represents the foreground color of the widget.
font It represents the font type of the widget text.
height It represents the height of the widget.
labelAnchor It represents the exact position of the text within the widget. The default is NW(north-west)
labelwidget It represents the widget to be used for the label. The frame uses the text for the label if no value specified.
highlightbackground The color of the focus highlight border when the widget doesn't have the focus.
highlightcolor The color of the focus highlight when the widget has the focus.
highlightthickness The width of the focus highlight border.
padx The horizontal padding of the widget.
pady The vertical padding of the widget.
relief It represents the border style. The default value is GROOVE.
text It represents the string containing the label text.
width It represents the width of the frame.

 

Examples

Here are some examples

 

from tkinter import *  
  
win = Tk()  
win.geometry("300x200")  
  
labelframe1 = LabelFrame(win, text="Frame label 1")  
labelframe1.pack(fill="both", expand="yes")  
  
toplabel = Label(labelframe1, text="This is the text for frame 1")  
toplabel.pack()  
  
labelframe2 = LabelFrame(win, text = "Frame label 2")  
labelframe2.pack(fill="both", expand = "yes")  
  
bottomlabel = Label(labelframe2, text = "This is the text for frame 2")  
bottomlabel.pack()  
  
win.mainloop()  

This displayed the following

and another example

from tkinter import *

win= Tk()  
win.geometry("400x400")

labelframe=LabelFrame(win,text="Maxpython example",width=300,  
             height=300,highlightcolor="yellow",  
             highlightbackground="blue",highlightthickness=15)

labelframe.grid(padx = 50, pady = 50)

win.mainloop() 

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More