栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

创建与“Clam”式ttk主题相同的自定义ttk样式(特定于按钮小部件)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建与“Clam”式ttk主题相同的自定义ttk样式(特定于按钮小部件)

使用此代码:

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()

但是由于某种原因,当我执行此操作时,该文本将不再出现在第一个按钮上…如果我知道了,我将再次进行编辑。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647865.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号