创建与 'clam' ttk 主题相同的自定义 ttk 样式(特定于按钮小部件)

Create custom ttk style same as 'clam' ttk Theme (button widget specific)

我需要为按钮小部件创建自定义样式,它与使用 ttk 'clam' 主题的按钮具有相同的外观。

我可以这样设置主题:

s = ttk.Style()
s.theme_use('clam')

但是,鉴于主题的性质,这会将所有 ttk 小部件设置为使用 'clam'。

我希望能够将某些 ttk 按钮设置为使用 clam 外观,而将其他按钮设置为使用默认 ttk。

我尝试查看 'TButton' 的布局和配置,同时使用 clam 主题,但主题似乎是样式的集合,我不确定如何 'map'基于蛤按钮样式的自定义样式。

使用此代码:

import Tkinter as tk
import ttk

def get_element_details(style):
    print('element: %s' % style)
    print('option: %s' % str(s.element_options(style)))
    layout = s.layout(style)
    for elem, elem_dict in layout:
        get_sub_element_details(elem, elem_dict)
    print(layout)

def get_sub_element_details(elem, _dict, depth=1):
    print('%selement: %s' % (''.join(['\t' for i in range(depth)]), elem))
    for key in _dict:
        if key != 'children':
            print('%s%s: %s' % (''.join(['\t' for i in range(depth+1)]), key, _dict[key]))
    print('%soption: %s' % (''.join(['\t' for i in range(depth+1)]), s.element_options(elem)))
    if 'children' in _dict:
        for child, child_dict in _dict['children']:
            get_sub_element_details(child, child_dict, depth+1)

root = tk.Tk()
widget = ttk.Button(root, text='test')
widget.grid(sticky='nesw')

style = widget.winfo_class()

s = ttk.Style()

print(s.theme_use())
print('normal theme')
get_element_details(style)

print('\nclam theme')
s.theme_use('clam')
get_element_details(style)

您可以获得有关小部件的所有布局和配置选项的详细信息。 在我的盒子(xp)上使用本机主题我得到这个输出:

element: TButton
option: ()
    element: Button.button
        sticky: nswe
        option: ()
        element: Button.focus
            sticky: nswe
            option: ()
            element: Button.padding
                sticky: nswe
                option: ('-padding', '-relief', '-shiftrelief')
                element: Button.label
                    sticky: nswe
                    option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')

我得到了 clam 主题:

element: TButton
option: ()
    element: Button.border
        border: 1
        sticky: nswe
        option: ('-bordercolor', '-lightcolor', '-darkcolor', '-relief', '-borderwidth')
        element: Button.focus
            sticky: nswe
            option: ('-focuscolor', '-focusthickness')
            element: Button.padding
                sticky: nswe
                option: ('-padding', '-relief', '-shiftrelief')
                element: Button.label
                    sticky: nswe
                    option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')

请注意,clam 主题有一个带选项的 Button.border 元素,而原生主题有一个没有选项的 Button.button 元素。

您可以从 clam 主题保存布局(在写入时,或者您可以在 运行 期间通过加载 clam 主题获取布局,获取布局然后切换主题并重新加载布局)并使用它来设置按钮的样式。

编辑 理论上这应该有效:

import Tkinter as tk
import ttk

root = tk.Tk()

style = 'TButton'

s = ttk.Style()

#s.theme_use('clam')

#get_element_details(style)

clambuttonlayout = [('Button.border', {'border': '1', 'children': [('Button.focus', {'children': [('Button.padding', {'children': [('Button.label', {'sticky': 'nswe'})], 'sticky': 'nswe'})], 'sticky': 'nswe'})], 'sticky': 'nswe'})]

s.layout('clam.TButton', clambuttonlayout)

b1 = ttk.Button(root, text="Button 1", style='clam.TButton')
b1.grid()
b2 = ttk.Button(root, text="Button 2", style='TButton')
b2.grid()

root.mainloop()

但是由于某些原因,当我这样做时,文本不再出现在第一个按钮上... 如果我弄明白了,我会再次编辑。