According to the accepted answer on this post the use of .configure(highlightbackground='red')
on a button should apply a color around the button however in testing I cannot reproduce what the poster has demonstrated in their gif recording.
Here is my test case: (Note even copy pasting the posters code I cannot get the highlight effect they are showing)
import tkinter as tk
root = tk.Tk()
btn = tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=4, activebackground="#ffffff",
activeforeground="#000000", highlightbackground='red', highlightcolor='red')
btn.pack()
btn.focus_set()
root.mainloop()
Resulting app:
With some extensive searching I am not finding much on highlightbackground
in the way of Q/A about the same issue so maybe something is missing. I have also tried to set the focus as this documentation states the widget needs focus with the same non result.
Maybe it could be version or OS related...
OS - Windows 10 Pro
Python - 3.6.2
Updated example using Krrr's post. So this does kinda work now however the issue at hand here is that it is resizing the button and not providing the correct highlighted color.
import tkinter as tk
def ResponsiveWidget(widget, *args, **kwargs):
bindings = {
'<FocusIn>': {'highlightbackground': 'red', 'highlightcolor':'red'},
'<FocusOut>': {'highlightbackground': '#d9d9d9', 'highlightcolor':'SystemButtonFace'},
'<Enter>': {'state': 'active'},
'<Leave>': {'state': 'normal'}
}
for k, v in bindings.items():
root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))
def update_active(event):
global previous_button
if previous_button != event.widget:
previous_button.config(default='normal')
event.widget.config(default='active')
previous_button = event.widget
root = tk.Tk()
button_list = []
previous_button = None
for i in range(5):
if i == 0:
button_list.append(tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=5,
activebackground="#ffffff", activeforeground="#000000", default='active'))
previous_button = button_list[-1]
else:
button_list.append(tk.Button(root, text='test', bg="#000000", fg="#ffffff", highlightthickness=5,
activebackground="#ffffff", activeforeground="#000000", default='normal'))
button_list[-1].pack(padx=5, pady=5)
button_list[-1].bind('<ButtonRelease-1>', update_active)
root.mainloop()
Results:
Expectation: