主题 ttk 小部件

Theming ttk widgets

我正在尝试做一些简单的事情 -- 在 Python 脚本中更改 ttk 条目小部件的填充颜色以显示无效条目。我的问题不是具体如何执行此操作(尽管我想知道!),而是如何在给定普遍可用的文档和资源的情况下弄清楚如何执行此操作。

我已经阅读了我能找到的关于 ttk 和样式的所有内容(包括几个 Whosebug 问题),虽然如果您知道所有 keywords/options 所需的内容,应用样式的过程看起来很简单,但我不能如果 keywords/options 未知,请找出解决问题的方法。

对于这个问题的公认答案,我想看看如何设置 ttk Entry 小部件的背景颜色的过程,这会导致与 Python 一起使用的解决方案。我想学钓鱼。

首先,SO 并不是为了替代常规 documentation or tutorials 而设计的。但我同意 OP 的观点,即 ttk 缺少一点整体描述。

import tkinter as tk
import tkinter.ttk as ttk
from ttkthemes import ThemedStyle

ttk 不是由 tkinter 本身导入的,需要显式导入。建议导入为ttk,避免名称冲突

style = ttk.Style(root)
style.theme_use('default')

style class 旨在操纵样式数据库。 始终使用主题,即使您没有定义主题。可以找到主题列表 here or here. Or you can define your own with theme_create, like

button = ttk.Button(root, text='abc')
button.pack(expand=True, fill='both')
style_name = button.winfo_class()

每个 ttk.widget 都有样式选项,可以通过 .winfo_class

style.layout(style_name,
             [('Button.border', {'sticky': 'nswe', 'border': '1', 'children': [
                 ('Button.focus', {'sticky': 'nswe', 'children': [
                     ('Button.padding', {'sticky': 'nswe', 'children': [
                         ('Button.label', {'sticky': 'nswe'})
                         ]})
                     ]})
                 ]}
               )])

操纵布局为您提供了一些琐碎的选项,例如在右侧或左侧放置一个按钮。但除了 element_create it can be a powerfull tool. Like , Scrollbar.trough, .

print(style.layout(style_name))
print('Layout')

获取当前布局并尝试使用不同的主题,可以将它们与 element_create 混合在一起,或者尝试使用 windows/visual_styles_api 下的 vsapi

print(style.element_options('Button.label'))
print('label')
print(style.map(style_name))
print('map')

简单地configure some colors I have found, and linked in , a list of colors that can be configured but dosent mean they necessary have an effect

Options that are available don't necessarily have an effect, and it's not an error to modify a bogus option.

为您的入口背景配置 ttk::style configure TEntry -fieldbackground color,也可以在颜色列表中找到。

widget = ttk.Entry(root, text='abc', style='My.TEntry')
style.configure("My.TEntry",
                 foreground="grey",
                 fieldbackground="black")

示例:

import tkinter as tk
import tkinter.ttk as ttk
from ttkthemes import ThemedStyle

bg_color = '#091424'
fg_color = '#ffcfcf'
arrow_bg = '#8c101e'
border_color = '#db994c'
highlight_color = '#372940'
pressed_color = arrow_bg

root = tk.Tk()
style = ttk.Style(root)
style.theme_use('clam')#background/arrowcolor

frame =tk.Frame(root)
frame.pack()

widget = ttk.Spinbox(frame, text='abc', style="my.TSpinbox")
widget.pack(expand=True, fill='both')

style_name = widget.winfo_class()

style.layout('my.TSpinbox',
             [('Spinbox.field', {'sticky': 'nswe', 'children': [
                 ('Spinbox.background', {'sticky': 'nswe', 'children': [
                     ('Spinbox.padding', {'sticky': 'nswe', 'children': [
                         ('Spinbox.innerbg', {'sticky': 'nswe', 'children': [
                             ('Spinbox.textarea', {'expand': '1', 'sticky': 'nswe'})
                             ]})
                         ]}),
                     ('Spinbox.uparrow', {'side': 'top', 'sticky': 'nse'}),
                     ('Spinbox.downarrow', {'side': 'bottom', 'sticky': 'nse'})
                     ]})
                 ]})
              ])



style.configure('my.TSpinbox',
                background=bg_color,
                foreground=fg_color,
                arrowcolor=arrow_bg,
                arrowsize=20,
                relief="raised",
                bordercolor=border_color,
                lightcolor='red',
                darkcolor='green',
                troughcolor='pink',
                )

style.map("my.TSpinbox",
          arrowcolor = [('pressed', pressed_color),
                        ('active', highlight_color),
                        ('disabled', '#ffffff')])


print(style.layout(style_name))
print('Layout')
print(style.element_options('Spinbox.field'))
print('field')
print(style.element_options('Spinbox.downarrow'))
print('downarrow')
print(style.element_options('Spinbox.uparrow'))
print('uparrow')
print(style.element_options('Spinbox.padding'))
print('padding')
print(style.element_options('Spinbox.background'))
print('background')
print(style.element_options('Spinbox.textare'))
print('textarea')
print(style.element_options('Spinbox.innerbg'))
print('innerbg')
print(style.map(style_name))
print('map')

其他参考文献: