1

How do I set the border color (default, not if mouse is hovering above) of a button? I have searched the internet but found nothing.

from tkinter import *

root = Tk()
bindings = {
    '<FocusIn>': {'default': 'active'},
    '<FocusOut>': {'default': 'active'}
}

for k, v in bindings.items():
    root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))

root.title('Calculator')
root.config(bg='black')

x = Button(root, text='tgrsj', padx=50, bg='black', fg='lime',
           highlightcolor="blue",
           highlightbackground="blue",
           highlightthickness=10,
           relief=SOLID).grid(row=0, column=0)

root.mainloop()

here is the new image

  • What will happen if mouse is hovering above? – jizhihaoSAMA Nov 29 '20 at 10:17
  • nothing it should stay how it is –  Nov 29 '20 at 10:18
  • https://stackoverflow.com/questions/47156055/tkinters-button-cant-change-border-color – jizhihaoSAMA Nov 29 '20 at 10:39
  • Please stop replacing your question with entirely different parameters ; if you don't think opening another thread is necessary, then simply add "EDIT:" tags and add what you've tried since there. Your question here isn't that related to your title anymore, and it won't help people stumbling upon this thread in the future. – OctaveL Nov 29 '20 at 17:06

1 Answers1

1
x = Button(root, text='...', padx=50, bg='red', fg='black',
           highlightcolor="pink",
           highlightbackground="pink",
           highlightthickness=4,
           relief=SOLID)

Here's an example with a pink border of thickness 4, which works on Linux (and should work on macOS)

EDIT: It seems it's a Windows specific issue. It can be resolved by calling the following code after instantiating your root:

root = Tk()

bindings = {
    '<FocusIn>': {'default':'active'},
    '<FocusOut>': {'default': 'active'}
}

for k, v in bindings.items():
    root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))

root.title('Calculator')
root.config(bg='red')
Which I found in [this](https://stackoverflow.com/a/58396480/10489787) thread. In short, Windows isn't triggering events correctly, and here what we call ```highlightXXX``` actually have to do with what the button looks like when it's being focused or unfocused (here we want a pink border whether it's focused, with ```highlightcolor```, or unfocused, with ```highlightbackground```).

Instead, we modify Button's internal workings to make our own events which declare that focusing/unfocusing always activate the button, and so activate its pink border.

EDIT 2: An easier fix for Windows is simply to set the button as active by default.

root = Tk()

root.title('Calculator')
root.config(bg='red')

x = Button(root, text='...', padx=50, bg='red', fg='black',
           highlightcolor='blue',
           highlightbackground='blue',
           highlightthickness=4,
           relief=SOLID,
           default='active' #THIS LINE IS NEW
)

x.grid(row=0, column=0)
root.mainloop()
OctaveL
  • 1,017
  • 8
  • 18
  • pink border?? I got a black border. btw im using python 3 . myabe thats why? –  Nov 29 '20 at 12:22
  • @Ght007 That's odd. Are you sure you're using the exact same code? Can you send a picture of what you're getting? – OctaveL Nov 29 '20 at 12:50
  • I put the image in the queistion. –  Nov 29 '20 at 13:13
  • @Ght007 All right, the code I sent works fine on my Linux machine, but not on Windows 10 like yours. This is peculiar, I'll look into it and try to send working code. – OctaveL Nov 29 '20 at 14:26
  • @Ght007 I fixed it, you can check out my edit. – OctaveL Nov 29 '20 at 14:51
  • Honestly I'm a beginner (3 months exp) in python and don't know how to implement lambda functions or so and just started with tkinter yesterday. Could you tell me where exactly the new code goes? –  Nov 29 '20 at 15:05
  • @Ght007 Don't worry, you don't have to understand exactly what the code is doing for now. I had said to put it after instantiating ```root``` : I modified my code to make that a bit clearer (in short, between your first and second lines of code). – OctaveL Nov 29 '20 at 15:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225283/discussion-between-ght007-and-octavel). –  Nov 29 '20 at 15:18