【问题标题】:tkinter ttk widgets ignoring background color?tkinter ttk小部件忽略背景颜色?
【发布时间】:2014-07-08 03:28:47
【问题描述】:

我正在为应用程序使用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')

也不起作用(即没有变化)。 有什么想法吗?

【问题讨论】:

  • 我猜,你用的是Mac系统。
  • 是的,我在 10.9。这是 OS X 的已知问题吗?
  • 是的。至少是这样。我没有使用 OS X,所以不确定它是否仍然存在。
  • ttk 标签背景在 Windows 上工作正常。
  • 任何使用 tk 的人都应该尽可能使用最新的 tcl/tk 版本,包括最新的功能,也许更重要的是,最新的错误修复。 python.org 3.7.0b3 安装程序包含并安装 Python 使用 tcl/tk 8.6.8。与 8.5 的最后一个错误修复相比,这应该是一个很大的改进,这是 3.7 之前 Mac 上最新的 tcl/tk 支持。它应该有几个针对 macOS 的修复程序。这个问题是否解决了,我不知道。

标签: python tkinter


【解决方案1】:

我也遇到了这个问题,我相信问题是 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') 以查看问题。

【讨论】:

  • 确实,将主题切换为“经典”可以解决问题。
【解决方案2】:

我也有,我觉得是ttk的bug,是部分电脑造成的,无法修复。只需在背景中使用具有背景颜色的绘图功能有一个大矩形。我也想不出别的了。

【讨论】:

    【解决方案3】:

    2018 年更新:tkinter.ttk.Label 实例仍然不尊重“背景”配置选项,因此我暂时切换回使用 tkinter.Label 并将其作为错误提交给 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()
    

    【讨论】:

    • 更新:报告给 python bug-tracker,被告知 tkinter 模块是一个简单的 tk/tcl 包装器,并在那里报告错误报告,所以我已经向他们报告了错误。
    猜你喜欢
    • 2011-04-30
    • 2014-07-03
    • 2023-02-23
    • 2019-06-25
    • 2013-07-12
    • 2021-03-01
    • 1970-01-01
    • 2018-08-19
    • 2022-06-13
    相关资源
    最近更新 更多