Set style for Labelframe in Python Tkinter

TkinterServer Side ProgrammingProgrammingPython

Tkinter LabelFrame is similar to Frames in Tkinter Library. It works like a container where widgets can be placed. LabelFrame initially creates a container with some rectangular border around it. In order to style the LabelFrame widget, we have several style options such as background, borderwidth, labelanchor, highlightcolor and many more.

Example

In this example, we will see the LabelFrame widget and its properties.

#Import required libraries
from tkinter import *
#Create an instance of tkinter frame
win= Tk()
#Define the geometry of the window
win.geometry("750x250")
#Initialize a LabelFrame Widget
labelframe= LabelFrame(win, text= "Frame 01",width= 600, height= 200, labelanchor= "n", font= ('Helvetica 14 bold'),bd= 5, background="gray71", foreground= "white")
labelframe.pack(ipadx=10, ipady=20, expand= True, fill= BOTH)

#Create a Label inside LabelFrame
Label(labelframe, text= "I am inside a LabelFrame", font=('Helvetica15 bold'), foreground= "black").pack(pady= 20)

win.mainloop()

Output

Running the above code will display a window that contains a labelframe with borderwidth of 5px.

raja
Updated on 03-May-2021 11:46:40

Advertisements