繁体   English   中英

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

[英]tkinter ttk widgets ignoring background color?

我使用tkinter的主题( ttk )GUI工具包的应用程序。 尝试对主窗口中的小部件应用一些统一的样式:

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

字体更改效果很好,但是由于某些原因,窗口小部件(即ttk.Labelttk.Button )似乎无法反映背景更改,由于窗口背景与窗口小部件之间的对比,因此在视觉上非常明显。 如果我检查设置为:

label1.cget('background')

它返回'' ,所以很明显它没有被设置,但是鉴于ttk.Labelstyles的文档,我不明白这是怎么回事 尝试直接为单个标签设置背景:

label1.configure(background='#eeeeee')

也行不通(即没有变化)。 有任何想法吗?

我也遇到了这个问题,我相信问题是ttk的“ aqua”主题,这是OSX上的默认主题,它不考虑许多小部件中的背景颜色配置。 我通过将主题设置为“默认”来解决了该问题,该主题立即导致所有小部件的背景均按指定的方式显示。

这是我的基本示例:

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')以查看问题。

我也有这个问题,我认为这是ttk错误,是由某些计算机引起的,无法修复。 使用具有背景色的背景中的绘图功能,只需要一个大矩形即可。 我也没想到

2018更新:tkinter.ttk.Label实例仍然不尊重'background'配置选项,因此我暂时转回使用tkinter.Label并将其作为bug提交给python开发人员(至少将其从可用选项(如果它不遵守的话)。 我正在将Tk 8.6与python 3.6.5一起使用。 这是一个交互式会话的输出,以演示:

>>> 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