我需要为按钮小部件创建一个自定义样式,该样式与使用 ttk 'clam' 主题的按钮具有相同的外观。
我可以将主题设置为:
s = ttk.Style()
s.theme_use('clam')
但是,鉴于主题的性质,这会将所有 ttk 小部件设置为使用“clam”。
我希望能够设置某些 ttk 按钮以使用蛤蜊外观,而其他按钮则使用默认 ttk。
我曾尝试在使用蛤蜊主题时查看“TButton”的布局和配置,但似乎主题是样式的集合,我不确定如何根据蛤蜊按钮样式“映射”自定义样式.
我需要为按钮小部件创建一个自定义样式,该样式与使用 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
没有选项的元素。
您可以从 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()
但是由于某种原因,当我这样做时,文本不再出现在第一个按钮上……如果我弄清楚了,我会再次编辑。