Tkinter 小部件和/或系统默认颜色

时间:2021-04-22 11:40:15

标签: python tkinter

我正在尝试实现一个 tkinter 小部件,它具有与 Listbox 类似(最少)的功能,但允许多行条目。
基本上,它继承自 Frame,管理自己的滚动行为,并且每个项目都是该框架内的一个 Message

为了获得合理的外观和感觉,当点击一个项目时,我希望它像用户期望的那样突出显示。

对于此类任务,是否有使用系统/小部件默认颜色的好方法?
我发现 this page 向我暗示,系统颜色取决于平台。那是对的吗?或者有没有办法独立引用“正常高亮颜色​​”操作系统?

或者我应该为此手动实现颜色,这在我正在开发的系统上可能看起来不错,但很可能在其他系统上看起来很难看或至少不是有机的?

1 个答案:

答案 0 :(得分:2)

最简单的解决方案是创建一个临时列表框,然后让 tkinter 告诉您它使用的颜色。

listbox = tk.Listbox(root)
colors = {attr: listbox.cget(attr) for attr in (
    "background", "foreground", "disabledforeground",
    "highlightbackground", "highlightcolor",
    "selectbackground", "selectforeground"
)}

以上将生成列表框使用的颜色选项字典。在我的 OSX 机器上,它返回如下内容:

{
    'background': 'systemTextBackgroundColor',
    'disabledforeground': '#a3a3a3',
    'foreground': 'systemTextColor',
    'highlightbackground': 'systemWindowBackgroundColor',
    'highlightcolor': 'Black',
    'selectbackground': 'systemSelectedTextBackgroundColor',
    'selectforeground': 'systemSelectedTextColor'
}