-1

How do i change the colour of a border in tkinter

I have looked at other solutions which recommended using highlightcolor and highlightbackground, however these did not work.

excercises_button = Button(canvas, width=327, height=150, image=dumbell_img,borderwidth=4, relief="ridge", bg = "gray55", command = Excercises)
canvas_excercises_button = canvas.create_window(168, 724, window=excercises_button)

I would like the border of this button to be orange.

This is what it currently looks like: https://i.stack.imgur.com/3QX8X.png

Dominic Culyer
  • 99
  • 1
  • 2
  • 10
  • In order to create a border you will need to add the button to its own frame and use the background color of that frame to make a different color border from the button. – Mike - SMT Nov 01 '18 at 12:37
  • 1
    Possible duplicate of [No way to color the border of a Tkinter Button?](https://stackoverflow.com/questions/47352833/no-way-to-color-the-border-of-a-tkinter-button) – stovfl Nov 01 '18 at 14:22

3 Answers3

2

Here is an example of how one can have a kind of border created by using a frame and a button.

import tkinter as tk

root = tk.Tk()

frame = tk.Frame(root, highlightbackground="orange", highlightcolor="orange", highlightthickness=4, bd=0)
frame.grid(row=0, column=0)
# adding weights so the button is center on the frame.
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)

btn = tk.Button(frame,text="test", borderwidth=4, relief="ridge", bg = "gray55").grid(row=0, column=0)
root.mainloop()

Results:

enter image description here

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • unfortunately i can't use this method as it won't work very well with my wider application. Is there no attribute or single line of code which could add a small border? – Dominic Culyer Nov 01 '18 at 15:36
  • @DominicCulyer well there is not any simple way of doing this. It just doesnt exist in Tkinter. However I am sure a modified version of what I have shown you can and will work. Just takes some effort. There is more you can do with this. Like making sure that the border will always be a specific size around the widget even when resizing the window. Its just a matter of handling the configure event with the correct methods. – Mike - SMT Nov 01 '18 at 15:42
1

I found a solution!

What i did is I created a rectangle which surrounded the buttons.

canvas.create_rectangle(0, 638, 1100, 900, fill=colour)

I then made the three buttons a few pixels smaller to allow the background of the rectangle to show.

Dominic Culyer
  • 99
  • 1
  • 2
  • 10
0

Since this is the top answer on a google search, i will share the method found on the duplicate of this question:
(code refactored to work with python 3.10)

from tkinter import ttk,Tk
root = Tk()

style = ttk.Style(root)
style.theme_use('clam')
style.configure('my.TButton', bordercolor="red")

ttk_button = ttk.Button(root, text='Button', style='my.TButton')
ttk_button.pack()

root.mainloop()

Resulting window
this code works because certain ttk themes allow changing the border color on buttons.

LasevIX_
  • 3
  • 3