使用此代码:
import Tkinter as tkimport ttkdef 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: TButtonoption: () 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: TButtonoption: () 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 tkimport ttkroot = 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()但是由于某种原因,当我执行此操作时,该文本将不再出现在第一个按钮上…如果我知道了,我将再次进行编辑。



