2017-03-21 51 views
0

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

我可以设置喜欢的主题:

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

然而,由于主题的性质,这将然后设置所有TTK部件使用的“蚌壳”。

我希望能够设置某些ttk按钮来使用clam外观和其他人使用默认的ttk。

我试过看'TButton'的布局和配置,虽然蛤主题在使用,但它似乎是一个主题是样式的集合,我不确定如何'映射'基于自定义样式在蛤蜊按钮样式。使用

回答

1

验证码:

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) 

可以EGT有关widget的所有的布局和配置选项的详细信息。 对我的盒子(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') 

,并与蛤主题我得到:

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元素没有选项。

您可以保存蛤主题的布局(在写入时,或者您可以在运行时加载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() 
然而

由于某种原因,当我做这个文本不再的第一个按钮出现...... 如果我搞清楚我会再次编辑。

+0

嗨,詹姆斯。感谢您的回答!我假设你正在使用Python x3查看你的打印语句?当我运行你的代码时,出现以下错误:'print'%soption:%s'%(''.join(['\ t'for s in range(depth + 1)]),s.element_options(elem) ) AttributeError:'int'对象没有属性'element_options'' - 这可能是由于Py 3x或不同版本的Tkinter>? –

+0

对不起,这是一个python2与python3的区别,使用s作为索引覆盖了函数内部的样式对象,我已经将其更改为i,并在python2.7中进行了测试 –

+0

感谢James这真的很酷! - 你能否详细说明如何使用上面的输出手动为按钮设置样式? –