Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've been working through the Tkinter chapters in Programming Python and encountered a problem where the foreground and background colours of a button will not change. I am working on a Mac OS X 10.6 system with Python 2.6.1. The colours of a label will change, but not the colours of a button. For example:

from Tkinter import *  Label(None, text='label', fg='green', bg='black').pack() Button(None, text='button', fg='green', bg='black').pack()  mainloop() 

On my Mac system the colours of the label change, but the colours of the button do not. On a Windows system with Python 2.6.1 the colours of both the label and button change.

Anyone know what is going wrong?

I've checked Interface Builder and it appears that there is no option to change the foreground or background colour of a button in that tool. There is the ability to edit the foreground and background colours of a label.

The Mac OS X rendering system (Quartz?) may just not support (easily) changing the fg and bg of a button.

like image 898
Anthony Cramp Avatar asked Oct 07 '09 06:10

Anthony Cramp


People also ask

How do you change the color of a button in Python GUI?

We can also change the foreground color by using the 'fg' property of the button widget in Tkinter. We can also use the configure method to change the color. As we know that we can use the 'bg' and 'fg' property of the Button widget to change the color, we need to pass this variable while using the Button object.

What is foreground color in Tkinter?

foreground − Foreground color for the widget. This can also be represented as fg. highlightbackground − Background color of the highlight region when the widget has focus. highlightcolor − Foreground color of the highlight region when the widget has focus.

How do you change the background color on OSX?

In the Preview app on your Mac, choose Preview > Preferences, then click General. Click the “Window background” colour well, then choose a colour.


2 Answers

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

like image 64
Luka Kerr Avatar answered Oct 09 '22 11:10

Luka Kerr


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.

like image 44
Bryan Oakley Avatar answered Oct 09 '22 10:10

Bryan Oakley