Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkinter ttk widgets ignoring background color?

Tags:

python

tkinter

I'm using tkinter's themed (ttk) GUI toolkit for an application. Trying to apply some uniform styling to the widgets in the main window:

s = ttk.Style()
s.configure('.', background='#eeeeee')
s.configure('.', font=('Helvetica', 14))
self.configure(background='#eeeeee')

The font change works great, but for some reason the widgets (i.e. ttk.Label and ttk.Button) don't seem to reflect the background change, which is pretty obvious visually due to contrast between the window's background and the widget's. If I check what it's set to:

label1.cget('background')

it returns '', so clearly it's not being set, but I don't understand what's wrong given the docs for ttk.Label and styles. Trying to set the background for a single label directly:

label1.configure(background='#eeeeee')

also doesn't work (i.e. no change). Any ideas?

like image 269
Alex Z Avatar asked May 20 '14 02:05

Alex Z


People also ask

How do I change the background color in TTK?

We can set the background color, foreground color, and other attributes of the Combobox widget by visiting the configure function in ttk and passing 'TCombobox' as the first parameter.

What we used to change the background Colour any widget?

ttk module is used for styling the tkinter widgets such as setting the background color, foreground color, activating the buttons, adding images to labels, justifying the height and width of widgets, etc. In order to add a background color in tkinter widgets, we can specify the background property in the widget.

How do you style a TTK button?

To add styling in a ttk. Button we have to first create an object of style class which is available in tkinter. ttk. We can create ttk.

Which option is used to foreground color for the widget when the widget is active?

activeforeground − Foreground color for the widget when the widget is active.


2 Answers

I was having this problem as well, and I believe the issue is ttk's "aqua" theme, which is the default on OSX, doesn't respect background colour configuration in a number of widgets. I solved the problem by setting the theme to "default", which immediately caused all widgets' backgrounds to appear as specified.

Here's my basic example:

import tkinter
from tkinter import ttk

root = tkinter.Tk()
style = ttk.Style(root)
style.theme_use('classic')
style.configure('Test.TLabel', background= 'red')
text = ttk.Label(root, text= 'Hello', style= 'Test.TLabel')
text.grid()
root.mainloop()

Try changing style.theme_use('classic') to style.theme_use('aqua') to see the issue.

like image 187
gamecatw Avatar answered Oct 21 '22 15:10

gamecatw


I had that too, I think it is a ttk bug, is caused by some computers and can't be fixed. Just have a big rectangle using the draw function in the background having the background color. I can't think of anything else, either.

like image 27
Hans Avatar answered Oct 21 '22 15:10

Hans