How to put a border around a Frame in Python Tkinter?

To put a border around a Frame in Tkinter, we can use the highlightbackground and highlightthickness parameters while creating the Frame. Alternatively, we can use relief and borderwidth options for more border styles.

Method 1: Using highlightbackground and highlightthickness

The highlightbackground parameter sets the border color, while highlightthickness controls the border width ?

from tkinter import *

root = Tk()
root.geometry("400x300")
root.title("Frame Border Example")

# Create frame with blue border
frame1 = Frame(root, highlightbackground="blue", highlightthickness=2)
frame1.pack(padx=20, pady=20, fill="both", expand=True)

# Add some widgets inside the frame
Label(frame1, text="Music Player", font=("Arial", 14, "bold")).pack(pady=10)

Checkbutton(frame1, text="Music", anchor="w").pack(padx=10, pady=5, fill="x")
Checkbutton(frame1, text="Video", anchor="w").pack(padx=10, pady=5, fill="x")
Checkbutton(frame1, text="Songs", anchor="w").pack(padx=10, pady=5, fill="x")
Checkbutton(frame1, text="Games", anchor="w").pack(padx=10, pady=5, fill="x")

Button(frame1, text="Play", font=("Arial", 12, "bold")).pack(pady=10)

root.mainloop()

Method 2: Using relief and borderwidth

The relief parameter provides different border styles like raised, sunken, groove, and ridge ?

from tkinter import *

root = Tk()
root.geometry("500x400")
root.title("Different Border Styles")

# Different border styles
styles = [("raised", "red"), ("sunken", "green"), ("groove", "blue"), ("ridge", "orange")]

for i, (relief_style, bg_color) in enumerate(styles):
    frame = Frame(root, relief=relief_style, borderwidth=3, bg=bg_color)
    frame.pack(padx=10, pady=10, fill="x")
    
    Label(frame, text=f"{relief_style.capitalize()} Border", 
          font=("Arial", 12, "bold"), bg=bg_color).pack(pady=10)

root.mainloop()

Method 3: Using LabelFrame for Automatic Borders

LabelFrame automatically creates a border around the frame with an optional text label ?

from tkinter import *

root = Tk()
root.geometry("400x300")
root.title("LabelFrame Border")

# LabelFrame with automatic border
label_frame = LabelFrame(root, text="Settings", font=("Arial", 12, "bold"), 
                        relief="groove", borderwidth=2)
label_frame.pack(padx=20, pady=20, fill="both", expand=True)

# Add widgets inside LabelFrame
Checkbutton(label_frame, text="Enable notifications").pack(anchor="w", padx=10, pady=5)
Checkbutton(label_frame, text="Auto-save").pack(anchor="w", padx=10, pady=5)
Checkbutton(label_frame, text="Dark mode").pack(anchor="w", padx=10, pady=5)

Button(label_frame, text="Apply Settings").pack(pady=10)

root.mainloop()

Comparison of Border Methods

Method Parameters Best For
highlightbackground highlightbackground, highlightthickness Simple colored borders
relief relief, borderwidth 3D border effects
LabelFrame text, relief, borderwidth Grouped widgets with labels

Conclusion

Use highlightbackground for simple colored borders, relief for 3D effects, or LabelFrame for automatic borders with labels. Choose the method that best fits your application's design requirements.

Updated on: 2026-03-26T18:35:01+05:30

37K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements