How to set border color of certain Tkinter widgets?
Asked Answered
M

2

36

I'm trying to change the background color of my Tkinter app, but for certain widgets it leaves a white border around the edges.

For example, this:

from tkinter import *

COLOR = "black"

root = Tk()
root.config(bg=COLOR)

button = Button(text="button", bg=COLOR)
button.pack(padx=5, pady=5)
entry = Entry(bg=COLOR, fg='white')
entry.pack(padx=5, pady=5)
text = Text(bg=COLOR, fg='white')
text.pack(padx=5, pady=5)

root.mainloop()

How can I set border colour of certain Tkinter widgets?

Marlee answered 1/12, 2010 at 2:38 Comment(0)
M
53

Just use

widget.config(highlightbackground=COLOR)

Furthermore, if you don't want that border at all, set the highlightthickness attribute to 0 (zero).

Marlee answered 1/12, 2010 at 4:10 Comment(4)
I wish this nice solution worked for all widgets, but Combobox widgets, for example, don't have a highlightbackground option. So the solution applied to a combobox raises tkinter.TclError: unknown option "-highlightbackground".Indispose
How is highlightbackground related with the border of the background, that it is supposed to work even if widget is not highlighted?Choler
@Choler Any suggestions? I ran into that same problem. I ned to change the border colour not the highlightbackgroundLicking
@Choler Found an answer. Putting it in a frame with bg=... and then using .pack(padx=..., pady=...) on the widget.Licking
Z
5

You have to set the two highlights (with and without focus) to have a continuous color.

from tkinter import *
root = Tk()
e = Entry(highlightthickness=2)
e.config(highlightbackground = "red", highlightcolor= "red")
e.pack()
root.mainloop()
Zebe answered 13/4, 2020 at 13:59 Comment(1)
It works very well in ubuntu but when I tried same code in windows it doesn't work, it just give black border around it, can you please suggest any answer ? self.historyButton = Button(self.M_Frame, text = 'History Logs', activebackground='#0892d0', activeforeground='white', bd=0, relief='solid', bg='#222222',fg='white', highlightbackground ='#0892d0', highlightcolor ='#0892d0', font=('Flux Regular', 10, 'bold'), command = self.logHistoryToConsole) self.historyButton.grid(row=0, column = 0, padx=2)Hohenlohe

© 2022 - 2024 — McMap. All rights reserved.