import tkinter as tk
root = tk.Tk()
# Create a frame to hold the button with the desired border color
button_frame = tk.Frame(root, background="black", bd=2) # You can adjust 'bd' for border thickness
button_frame.pack(padx=50, pady=50) # Extra padding to better visualize the button
# Place the button inside the frame
button = tk.Button(button_frame, text="Button with Side Borders", highlightbackground="white", bd=0)
button.pack(side="left", expand=True, fill="both")
# Run the application
root.mainloop()
Alternatively, if you are using themed widgets from ttk, you can define a custom style that applies a left and right border using element layering. The example below demonstrates this:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
style = ttk.Style()
# Create a new style that inherits from the default TButton style
style.layout('LeftRightBorder.TButton', [
('Button.border', {'sticky': 'nswe', 'children': [
('Button.padding', {'sticky': 'nswe', 'children': [
('Button.label', {'sticky': 'nswe'})
]}),
]}),
])
# Configure the new style to only show left and right borders
style.configure('LeftRightBorder.TButton', background='SystemButtonFace')
style.configure('LeftRightBorder.TButton', bordercolor='black')
style.configure('LeftRightBorder.TButton', lightcolor='black') # Left color
style.configure('LeftRightBorder.TButton', darkcolor='black') # Right color
style.configure('LeftRightBorder.TButton', borderwidth=2)
# Create the button and assign the new style to it
button = ttk.Button(root, text="Button with Side Borders", style='LeftRightBorder.TButton')
button.pack(padx=20, pady=20)
root.mainloop()