创建与“蛤"式ttk主题相同的自定义ttk样式(特定于按钮小部件) [英] Create custom ttk style same as 'clam' ttk Theme (button widget specific)

查看:266
本文介绍了创建与“蛤"式ttk主题相同的自定义ttk样式(特定于按钮小部件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用ttk'clam'主题为按钮小部件创建一种与按钮外观相同的自定义样式.

I need to create a custom style for button widgets which has the same appearance as buttons using the ttk 'clam' theme.

我可以将主题设置为:

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

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

However, given the nature of a theme, this will then set all ttk widgets to use 'clam'.

我希望能够设置某些ttk按钮以使用蛤lam外观,而其他按钮则可以使用默认ttk.

在尝试使用蛤theme主题时,我尝试查看"TButton"的布局和配置,但似乎主题是样式的集合,我不确定如何根据样式来映射"自定义样式蛤button按钮样式.

I have tried looking at the layouts and configurations of 'TButton' whilst the clam theme is in use but it seems that a theme is a collection of styles and I am unsure on how to 'map' a custom style based on the clam button style.

推荐答案

使用此代码:

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)上使用本机主题,我得到以下输出:

you can egt details about all the layout and config options of the widget. with the native theme on my box (xp) i get this output:

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')

以蛤lam为主题,我得到了:

and with the clam theme i get:

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')

请注意,蛤主题使用带有选项的Button.border元素,而原生主题使用没有选项的Button.button元素.

note that the clam theme has a Button.border element with options, where the native theme has a Button.button element with no options.

您可以从clam主题中保存布局(在编写时,也可以在运行时通过加载clam主题,获取布局然后切换回主题并重新加载布局来获取布局),然后使用它来设置样式按钮.

you can save the layout from the clam theme (either at write time, or you can get it during run time by loading clam theme, fetch layout then switch theme back and load the layout back in) and use that to style the button.

编辑 从理论上讲,这应该起作用:

EDIT in theory this should work:

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()

但是由于某些原因,当我执行此操作时,文本不再出现在第一个按钮上... 如果我知道了,我会再次编辑.

however for some reason when I do this the text no longer appears on the first button... if i figure it out i'll edit again.

这篇关于创建与“蛤"式ttk主题相同的自定义ttk样式(特定于按钮小部件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆