Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set the border color of certain Tkinter widgets?
Sometimes you need to customize the appearance of Tkinter widgets by changing their border colors. You can achieve this using the highlightcolor and highlightbackground properties to control border appearance in different states.
Understanding Border Properties
Tkinter widgets have several properties that control border appearance:
- highlightcolor ? Border color when the widget has focus
- highlightbackground ? Border color when the widget doesn't have focus
- highlightthickness ? Width of the highlight border (default is usually 0)
Basic Example
Here's how to create an Entry widget and change its border color dynamically ?
# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Set the geometry of frame
win.geometry("600x250")
# Define a function to change the color of entry widget
def change_color():
text.config(highlightthickness=2, highlightbackground="red")
# Create an Entry widget for which we want to change the border color
text = Entry(win, width=50)
text.pack()
# Create a Button Widget
button = Button(win, text="Change", command=change_color)
button.pack(pady=20)
win.mainloop()
Setting Initial Border Colors
You can also set border colors when creating the widget ?
from tkinter import *
win = Tk()
win.geometry("600x300")
# Entry with blue border when focused, gray when not focused
entry1 = Entry(win, width=40, highlightcolor="blue",
highlightbackground="gray", highlightthickness=2)
entry1.pack(pady=10)
# Entry with green border styling
entry2 = Entry(win, width=40, highlightcolor="green",
highlightbackground="lightgray", highlightthickness=3)
entry2.pack(pady=10)
# Text widget with custom border
text_widget = Text(win, width=40, height=5, highlightcolor="purple",
highlightbackground="yellow", highlightthickness=2)
text_widget.pack(pady=10)
win.mainloop()
Multiple Color Changes
You can create functions to change borders to different colors ?
from tkinter import *
win = Tk()
win.geometry("600x350")
# Create Entry widget
entry = Entry(win, width=50, highlightthickness=2)
entry.pack(pady=20)
# Functions to change border colors
def set_red_border():
entry.config(highlightbackground="red", highlightcolor="darkred")
def set_blue_border():
entry.config(highlightbackground="blue", highlightcolor="darkblue")
def set_green_border():
entry.config(highlightbackground="green", highlightcolor="darkgreen")
def reset_border():
entry.config(highlightbackground="SystemButtonFace", highlightcolor="SystemWindowText")
# Create buttons for different colors
Button(win, text="Red Border", command=set_red_border, bg="lightcoral").pack(pady=5)
Button(win, text="Blue Border", command=set_blue_border, bg="lightblue").pack(pady=5)
Button(win, text="Green Border", command=set_green_border, bg="lightgreen").pack(pady=5)
Button(win, text="Reset Border", command=reset_border, bg="lightgray").pack(pady=5)
win.mainloop()
Key Points
- Set
highlightthicknessto a value greater than 0 to make borders visible - Use
highlightcolorfor the focused state andhighlightbackgroundfor the unfocused state - These properties work with Entry, Text, Listbox, and other input widgets
- You can use standard color names like "red", "blue" or hex codes like "#FF0000"
Conclusion
Use highlightbackground and highlightcolor along with highlightthickness to customize widget border colors. This allows you to create visually appealing interfaces and provide visual feedback to users when widgets gain or lose focus.
