简体   繁体   English

Tkinter的TTK小部件忽略背景色?

[英]tkinter ttk widgets ignoring background color?

I'm using tkinter 's themed ( ttk ) GUI toolkit for an application. 我使用tkinter的主题( ttk )GUI工具包的应用程序。 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 (ie 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. 字体更改效果很好,但是由于某些原因,窗口小部件(即ttk.Labelttk.Button )似乎无法反映背景更改,由于窗口背景与窗口小部件之间的对比,因此在视觉上非常明显。 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 . 它返回'' ,所以很明显它没有被设置,但是鉴于ttk.Labelstyles的文档,我不明白这是怎么回事 Trying to set the background for a single label directly: 尝试直接为单个标签设置背景:

label1.configure(background='#eeeeee')

also doesn't work (ie no change). 也行不通(即没有变化)。 Any ideas? 有任何想法吗?

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. 我也遇到了这个问题,我相信问题是ttk的“ aqua”主题,这是OSX上的默认主题,它不考虑许多小部件中的背景颜色配置。 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. 尝试将style.theme_use('classic')更改为style.theme_use('aqua')以查看问题。

I had that too, I think it is a ttk bug, is caused by some computers and can't be fixed. 我也有这个问题,我认为这是ttk错误,是由某些计算机引起的,无法修复。 Just have a big rectangle using the draw function in the background having the background color. 使用具有背景色的背景中的绘图功能,只需要一个大矩形即可。 I can't think of anything else, either. 我也没想到

2018 update: tkinter.ttk.Label instance still does not respect the 'background' configuration option, so I switched back to using tkinter.Label for the time being and am submitting it as a bug to python developers (at least remove it from the available options if it won't respect it). 2018更新:tkinter.ttk.Label实例仍然不尊重'background'配置选项,因此我暂时转回使用tkinter.Label并将其作为bug提交给python开发人员(至少将其从可用选项(如果它不遵守的话)。 I'm using python 3.6.5 with Tk 8.6. 我正在将Tk 8.6与python 3.6.5一起使用。 Here is an output of an interactive session to demonstrate: 这是一个交互式会话的输出,以演示:

>>> import tkinter as tk
>>> import tkinter.ttk as ttk
>>> root = tk.Tk()
>>> tk_label = tk.Label(root)
>>> tk_label.keys()
['activebackground', 'activeforeground', 'anchor', 'background', 'bd', 'bg', 'bitmap', 'borderwidth', 'compound', 'cursor', 'disabledforeground', 'fg', 'font', 'foreground', 'height', 'highlightbackground', 'highlightcolor', 'highlightthickness', 'image', 'justify', 'padx', 'pady', 'relief', 'state', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'wraplength']
>>> tk_label.config(text='Old style tkinter.Label instance', foreground='blue', background='red')
>>> tk_label.pack()
>>> new_ttk_label = ttk.Label(root)
>>> new_ttk_label.keys()
['background', 'foreground', 'font', 'borderwidth', 'relief', 'anchor', 'justify', 'wraplength', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'image', 'compound', 'padding', 'state', 'cursor', 'style', 'class']
>>> new_ttk_label.config(text='New tkinter.ttk.Label instance', foreground='blue', background='blue')
>>> new_ttk_label.pack()
>>> tk_label.config('background')
('background', 'background', 'Background', <border object: 'White'>, 'red')
>>> new_ttk_label.config('background')
('background', 'frameColor', 'FrameColor', '', <border object: 'blue'>)
>>> new_ttk_label.config('foreground')
('foreground', 'textColor', 'TextColor', '', <color object: 'blue'>)
>>> root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM