How to set the border color of certain Tkinter widgets?


Let us suppose we want to change the border color of a tkinter widget. We can configure the widget by passing the highlightcolor, highlightbackground property of the widget.

Example

In this example, we have created an Entry widget and a button that can be triggered to change the border color of the Entry Widget.

#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 a 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()

Output

Running the above code will display a window that contains a button which can be used to change the border color of the entry widget.

Now click on “Change” Button to change the border color of Widget.

Updated on: 26-Mar-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements