我正在为一个应用程序使用tkinter
的主题(ttk
)图形用户界面工具包,试图对主窗口中的小部件应用一些统一的样式:
s = ttk.Style()
s.configure('.', background='#eeeeee')
s.configure('.', font=('Helvetica', 14))
self.configure(background='#eeeeee')
字体更改效果很好,但由于某些原因,小部件(即ttk.Label
和ttk.Button
)似乎不能反映背景变化,这在视觉上非常明显,因为窗口的背景和小部件的背景之间的对比度。
label1.cget('background')
它返回''
,所以很明显它没有被设置,但是我不明白ttk.Label和styles的文档出了什么问题。尝试直接设置单个标签的背景:
label1.configure(background='#eeeeee')
也不起作用(也就是没有变化)。有什么想法吗?
发布于 2018-05-18 12:03:02
我也遇到了这个问题,我认为问题是ttk的"aqua“主题,这是OSX上的默认主题,在许多小部件中不尊重背景颜色配置。我通过将主题设置为"default“解决了这个问题,这会立即使所有小部件的背景都按指定的方式显示。
下面是我的基本示例:
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')
以查看问题。
发布于 2014-05-20 03:10:12
我也有,我认为这是一个ttk错误,是由一些计算机引起的,无法修复。只需在背景中使用绘图功能创建一个具有背景颜色的大矩形即可。我也想不出其他的东西了。
发布于 2018-04-27 14:17:35
2018年更新: python实例仍然不尊重‘后台’配置选项,所以我暂时切换回使用tkinter.Label,并将其作为错误提交给tkinter.ttk.Label开发人员(如果它不尊重它,至少将其从可用选项中删除)。我使用的是带有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()
https://stackoverflow.com/questions/23750141
复制相似问题