How to change border color in tkinter widget?

Question:

I want to know how should I chnage border color of tkinter Label or Button , I put the releif in solid and the border color would be black . i have tried highlightthickness,highlightcolor,highlightbackground but it doesnt work

here is a sample of my code :

import tkinter as tk 

root = tk.Tk()
root.geometry("800x450")
root.title("How should i change border color")
tk.Label(root,text = "How should i change border color",width = 50 , height = 4 ,bg = "White",relief = "solid").place(x=10,y=10)
tk.Button(root,text = "Button",width = 5 , height = 1 ,bg = "White",relief = "solid").place(x=100,y=100)


root.mainloop()

and here is what i want to change(the border color that is Black now , I want to change it to red) :

image

I have tried What you said @moshe-perez but it doesnt work:
image

Asked By: A._.P._.G

||

Answers:

When you use highlightbackground you need to provide a color code, for example "#37d3ff".
So use highlightbackground="COLORCODE" and remove relief="solid"

For example:

import tkinter as tk

root = tk.Tk()
root.geometry("800x450")
root.title("How should i change border color")
tk.Label(root, text="How should i change border color", width=50, height=4, bg="White", highlightthickness=4, highlightbackground="#37d3ff").place(x=10, y=10)
tk.Button(root, text="Button", width=5, height=1, bg="White", highlightbackground="#37d3ff").place(x=100, y=100)


root.mainloop()

result:

enter image description here

Update: While it workes on my ubuntu machine, I just checked it on windows and it doesn’t work there.

Answered By: Moshe perez

AFAIK, there is no way in tkinter to change the border color. One of the workarounds I used was making a slightly bigger label in root, and putting my label into that.

import tkinter as tk

root = tk.Tk()
root.geometry("800x450")
root.title("How should i change border color")
border = tk.Label(root, width=52, height=5, bg='red')
border.place(x=10, y=10)
tk.Label(border, text="How should i change border color", width=50, height=4, bg="White", highlightthickness=4, highlightbackground="#37d3ff").place(x=1, y=1)
tk.Button(root, text="Button", width=5, height=1, bg="White", highlightbackground="#37d3ff").place(x=100, y=100)


root.mainloop()

Not pretty, but it works.
enter image description here

Answered By: Daniel Zaksevski

this might be helpful, as I used Frame

I could be able to change the background color.

import tkinter as tk 

root = tk.Tk()
root.geometry("800x450")
root.title("How should i change border color")
border = tk.Frame(root, background="green")
label = tk.Label(border, text="How should i change border color", bd=5)
label.pack(fill="both", expand=True, padx=5, pady=5)
border.pack(padx=20, pady=20)
button1 = tk.Button(root, background="green")
name = tk.Button(button1, text="click", bd=0)
name.pack(fill="both", expand=True, padx=2, pady=2)
button1.pack(padx=20, pady=20)


root.mainloop()

Lengthy but worked.

Answered By: Kartheek