我需要为按钮小部件创建一个自定义样式,它的外观与使用ttk 'clam‘主题的按钮相同。
我可以把主题设为:
s = ttk.Style()
s.theme_use('clam')
但是,考虑到主题的性质,这将将所有ttk小部件设置为使用“clam”。
我希望能够设置某些ttk按钮以使用clam外观,而其他按钮则可以使用默认ttk.。
我试着查看“TButton”的布局和配置,而clam主题正在使用,但似乎一个主题是一个样式的集合,我不确定如何‘映射’一个基于clam按钮样式的定制样式。
发布于 2017-05-25 14:04:39
使用此代码:
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()
但是,由于某种原因,当我这样做时,文本不再出现在第一个按钮上.如果我搞清楚了,我会再编辑一遍。
https://stackoverflow.com/questions/42931533
复制相似问题