0

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
A._.P._.G
  • 92
  • 1
  • 10

3 Answers3

1

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.

Moshe perez
  • 1,626
  • 1
  • 8
  • 17
0

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

Daniel Zaksevski
  • 68
  • 1
  • 2
  • 8
0

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.

Kartheek
  • 164
  • 2
  • 17
  • its ok for label but when you set a button under another one if user click on the button nothing would happen to the border(button1) and when user push the border(button1)... – A._.P._.G Feb 06 '20 at 15:03