Open In App

How to Change Tkinter LableFrame Border Color?

Last Updated : 01 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

LableFrame in Tkinter is the widget that creates the rectangular area which contains other widgets. In this article, we are going to see how we can change the border of the label frame. But for achieving the color, we need to go through the theme for Tkinter, hence we use ttk module for the same which is inbuilt in python 3 Tkinter.

Step by Step Implementation:

Step 1: Importing all the required modules

Python3




# import tkinter 
import tkinter as tk 
  
# import ttk theme module for styling
import tkinter.ttk as ttk


Step 2: Building the label frame and put some widgets inside it.

Python3




import tkinter as tk
import tkinter.ttk as ttk
  
# initialize the tkinter window
root = tk.Tk() 
  
# provide some width and height
root.geometry("300x300"
  
# created a label frame with title "Group"
labelframe = ttk.LabelFrame(root, text = "GFG")
  
# provide padding 
labelframe.pack(padx=30, pady=30
  
# created the text label inside the the labelframe
left = tk.Label(labelframe, text="Geeks for Geeks"
left.pack()
  
root.mainloop()


Output:

Example 1: Using Clam theme in Tkinter.

Clam is a theme for tkinter windows and widgets. It comes under ttk module. The syntax for creating clam ttk window:

style = ttk.Style()
style.theme_use('clam')

Note: We use a clam theme in ttk module which enables using properties like border colors. Other themes may not have properties like border color.

Below is the implementation: 

Python3




import tkinter as tk
import tkinter.ttk as ttk
  
root = tk.Tk()
  
# initialize style function
style = ttk.Style()
  
# Use clam theme
style.theme_use('clam')
  
# Used TLabelframe for styling labelframe widgets,
# and use red color for border
style.configure("TLabelframe", bordercolor="red")
  
labelframe = ttk.LabelFrame(root, text = "GFG")
labelframe.grid(padx = 30, pady = 30)
  
left = tk.Label(labelframe, text = "Geeks for Geeks")
left.pack()
  
root.mainloop()


Output:

Example 2: Building our own style functionality theme.

Syntax:

# creating the theme

style.theme_create(‘class_name’, # Name of the class                   

                   settings={         

                       ‘TLabelframe’: {  # Name of the widget 

                           ‘configure’: {  # Configure argument

                               ‘background’:’green’  # changing background argument

                           }

                       }

                     }

                   )

# For using the theme

style.theme_use(‘class_name’)

We created a style theme function with the help of ttk style. We use theme_create to create the functionality and can use that using theme_use for assigning the theme.

Below is the implementation: 

Python3




import tkinter as tk
import tkinter.ttk as ttk
  
# initialize the tkinter window
root = tk.Tk()
  
# initializing the style function
style = ttk.Style()
  
# creating the theme with the
# initializing the style function
style = ttk.Style()
  
# creating the theme with the
style.theme_create('style_class',
  
                   # getting the settings
                   settings={
  
                       # getting through the Labelframe
                       # widget
                       'TLabelframe': {
                           
                           # configure the changes
                           'configure': {
                               'background': 'green'
                           }
                       },
  
                       # getting through the Labelframe's 
                       # label widget
                       'TLabelframe.Label': {
                           'configure': {
                               'background': 'green'
                           }
                       }
                   }
                   )
style.theme_use('style_class')
  
# created a label frame with title "Group"
labelframe = ttk.LabelFrame(root, text="Group")
  
# provide padding
labelframe.pack(padx=30, pady=30)
  
# created the text label inside the the labelframe
left = tk.Label(labelframe, text="Geeks for Geeks")
  
left.pack()
  
root.mainloop()


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads