Python: How to put a border around an OptionMenu using Tkinter?

Adding a border around an OptionMenu widget in Tkinter can be achieved using the borderwidth and relief configuration options. These properties control the appearance of the border around the widget.

Basic OptionMenu with Border

The borderwidth parameter sets the thickness of the border, while relief determines the border style ?

import tkinter as tk
from tkinter import *

# Create an instance of Tkinter frame
root = tk.Tk()
root.geometry("400x300")
root.title("OptionMenu with Border")

# Create menu options
options = ("Cellphone", "Laptop", "Smartwatch", "Digital Camera")

# Create a StringVar to hold the selected value
selected_option = tk.StringVar(value=options[0])

# Create OptionMenu with border configuration
menu = OptionMenu(root, selected_option, *options)
menu.config(
    width=15,
    borderwidth=3,
    relief="raised",
    activebackground="lightblue"
)

menu.pack(pady=30)

root.mainloop()

Different Border Styles

Tkinter provides several relief styles for creating different border effects ?

import tkinter as tk

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

options = ("Option 1", "Option 2", "Option 3")

# Different relief styles
relief_styles = ["flat", "raised", "sunken", "groove", "ridge"]

for i, style in enumerate(relief_styles):
    selected = tk.StringVar(value=options[0])
    
    label = tk.Label(root, text=f"Relief: {style}")
    label.pack(pady=5)
    
    menu = OptionMenu(root, selected, *options)
    menu.config(
        width=12,
        borderwidth=3,
        relief=style,
        bg="white",
        activebackground="yellow"
    )
    menu.pack(pady=5)

root.mainloop()

Customized Border with Frame

For more advanced border customization, you can place the OptionMenu inside a Frame with custom styling ?

import tkinter as tk

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

# Create a frame with custom border
border_frame = tk.Frame(
    root,
    highlightbackground="blue",
    highlightthickness=2,
    bd=1,
    relief="solid"
)
border_frame.pack(pady=30, padx=30)

# Create OptionMenu inside the frame
options = ("Red", "Green", "Blue", "Yellow")
selected_color = tk.StringVar(value=options[0])

menu = OptionMenu(border_frame, selected_color, *options)
menu.config(
    width=10,
    borderwidth=0,  # Remove OptionMenu border since frame provides it
    bg="white"
)
menu.pack(padx=5, pady=5)

root.mainloop()

Configuration Options

Parameter Description Example Values
borderwidth Border thickness in pixels 1, 2, 3, 5
relief Border style appearance flat, raised, sunken, groove, ridge
highlightthickness Focus border thickness 0, 1, 2
bg Background color white, lightgray, #ffffff

Conclusion

Use borderwidth and relief properties to add borders directly to OptionMenu widgets. For advanced styling, wrap the OptionMenu in a Frame with custom border properties.

Updated on: 2026-03-27T07:41:10+05:30

865 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements