tkinter ttk 小部件忽略背景颜色? [英] tkinter ttk widgets ignoring background color?
问题描述
我正在为应用程序使用 tkinter
的主题 (ttk
) GUI 工具包.尝试对主窗口中的小部件应用一些统一的样式:
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')
字体更改效果很好,但由于某种原因,小部件(即 ttk.Label
和 ttk.Button
)似乎没有反映背景更改,这由于窗口背景和小部件之间的对比,在视觉上非常明显.如果我检查它的设置:
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')
它返回 ''
,所以很明显它没有被设置,但我不明白给定 ttk.Label 和 样式.尝试直接为单个标签设置背景:
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?
推荐答案
我也遇到了这个问题,我相信问题是 ttk 的aqua"主题,这是 OSX 上的默认主题,不尊重背景颜色在许多小部件中进行配置.我通过将主题设置为默认"解决了这个问题,这立即导致所有小部件的背景按指定显示.
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.
这是我的基本示例:
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()
尝试将 style.theme_use('classic')
更改为 style.theme_use('aqua')
以查看问题.
Try changing style.theme_use('classic')
to style.theme_use('aqua')
to see the issue.
这篇关于tkinter ttk 小部件忽略背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!