How to change the foreground or background colour of a Tkinter Button on Mac OS X?

There is a solution for changing the background of buttons on Mac.

Use:

highlightbackground=color

For example:

submit = Button(root, text="Generate", highlightbackground='#3E4149')

This results in the following, a nice button that fits in with the background:

Button


I think the answer is that the buttons on the mac simply don't support changing the background and foreground colors. As you've seen, this isn't unique to Tk.


For anyone else who happens upon this question as I did, the solution is to use the ttk module, which is available by default on OS X 10.7. Unfortunately, setting the background color still doesn't work out of the box, but text color does.

It requires a small change to the code:

Original:

from Tkinter import *

Label(None, text='label', fg='green', bg='black').pack()
Button(None, text='button', fg='green', bg='black').pack()

mainloop()

With ttk:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

# background="..." doesn't work...
ttk.Style().configure('green/black.TLabel', foreground='green', background='black')
ttk.Style().configure('green/black.TButton', foreground='green', background='black')

label = ttk.Label(root, text='I am a ttk.Label with text!', style='green/black.TLabel')
label.pack()

button = ttk.Button(root, text='Click Me!', style='green/black.TButton')
button.pack()

root.mainloop()

You can do it with tkmacosx library from PyPI.

Installation:

  • For Python 2, use pip install tkmacosx.

  • For Python 3, use pip3 install tkmacosx.


This is how you use tkmacosx:

from tkinter import *
from tkmacosx import Button

root = Tk()

B1 = Button(root, text='Mac OSX', bg='black',fg='green', borderless=1)
B1.pack()

root.mainloop()

It works fine on Mac OS X.

enter image description here


Its quite annoying that after years this is still a problem.

Anyways, as others have mentioned, highlightbackground (the border color) can be used in place of background on a Mac. If you increase the size of the border to be huge (the size of the button or greater), you will get a nice, solid background color. This will give your button the appearance of a label.

enter image description here

This works if you are using place, but not if you are using something like grid. With grid, increasing the border size increases the button size automatically, unfortunately.

However, if you must use grid, you can always hack it....create your colorless grid button. Next use place to parent a background color button on top of it. This will be the button with the 'command' on it or the button you bind events to.

If you want your code to be OS independent, you can either add an 'if OS == "Mac"' statement or even add a custom function that modifies the button if its on a Mac but leaves it alone on Windows or Linux. Here's the former:

from tkinter import *
import platform


if platform.system() == "Darwin":   ### if its a Mac
    B = Button(text="Refersh All Windows", highlightbackground="Yellow", fg="Black", highlightthickness=30)
else:  ### if its Windows or Linux
    B = Button(text="Refresh All Windows", bg="Yellow", fg="Black")

B.place(x=5, y=10, width=140, height=30)

mainloop()