2017-03-21 49 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這真的很酷! - 你能否詳細說明如何使用上面的輸出手動爲按鈕設置樣式? –