[英]Create custom ttk style same as 'clam' ttk Theme (button widget specific)
我需要為按鈕小部件創建一個自定義樣式,該樣式與使用 ttk 'clam' 主題的按鈕具有相同的外觀。
我可以將主題設置為:
s = ttk.Style()
s.theme_use('clam')
但是,鑒於主題的性質,這會將所有 ttk 小部件設置為使用“clam”。
我希望能夠設置某些 ttk 按鈕使用蛤蜊外觀和其他人使用默認 ttk。
我曾嘗試在使用蛤蜊主題時查看“TButton”的布局和配置,但似乎主題是一組樣式,我不確定如何根據蛤蜊按鈕樣式“映射”自定義樣式.
使用此代碼:
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')
和蛤蜊主題我得到:
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
元素。
您可以從蛤蜊主題保存布局(在寫入時,或者您可以在運行時通過加載蛤蜊主題獲取布局,獲取布局然后切換主題並重新加載布局)並使用它來設置按鈕的樣式。
理論上編輯這應該有效:
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()
但是由於某種原因,當我這樣做時,文本不再出現在第一個按鈕上......如果我弄清楚了,我會再次編輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.