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?
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.
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.
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.
activeforeground − Foreground color for the widget when the widget is active.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With