如何在 ttk.OptionMenu 周围制作边框
How to make border around ttk.OptionMenu
在尝试制作入口框架时,我 运行 遇到了一个问题,我无法在 ttk.OptionMenu 周围制作边框以使其看起来与 ttk.Entry 相似。 (图中相邻的两个)
制作选项菜单
option = ttk.OptionMenu(bottom_container, self.have, 'ANY', 'ANY', '0', '1', style='vista.TMenubutton')
option.grid(column=1, row=2, sticky='we')
我尝试使用样式(仍想使用 vista/winnative 外观)并且能够将选项菜单背景设为白色,但我找不到适合它周围边框的方法
这是我用来检查 ttk 小部件的代码,目的是弄清楚如何为它们设置主题:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
var = tk.StringVar()
widget = ttk.OptionMenu(root, var, 'ANY', 'ANY', '0', '1')
widget.grid(column=2, row=1, sticky='nesw')
style = widget.winfo_class()
s = ttk.Style()
#s.theme_use('clam')
elements = s.layout(style)
def get_element_details(elem, _dict, depth=1):
print('%selement: %s' % (''.join(['\t' for s in range(depth)]), elem))
for key in _dict:
if key != 'children':
print('%s%s: %s' % (''.join(['\t' for s in range(depth+1)]), key, _dict[key]))
print('%soption: %s' % (''.join(['\t' for s in range(depth+1)]), s.element_options(elem)))
if 'children' in _dict:
for child, child_dict in _dict['children']:
get_element_details(child, child_dict, depth+1)
print('element: %s' % style)
print('option: %s' % str(s.element_options(style)))
for elem, elem_dict in elements:
get_element_details(elem, elem_dict)
root.mainloop()
简单地说,如果不将主题切换为 clam 主题之类的东西,小部件中没有任何选项可以添加边框(我知道这很愚蠢)
我没有要测试的 vista 主题,但是我得到了 XP 主题:
element: TMenubutton
option: ()
element: Menubutton.dropdown
side: right
sticky: ns
option: ()
element: Menubutton.button
expand: 1
sticky: nswe
option: ()
element: Menubutton.padding
expand: 1
sticky: we
option: ('-padding', '-relief', '-shiftrelief')
element: Menubutton.label
sticky:
option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')
和蛤蜊主题:
element: TMenubutton
option: ()
element: Menubutton.border
sticky: nswe
option: ('-bordercolor', '-lightcolor', '-darkcolor', '-relief', '-borderwidth')
element: Menubutton.focus
sticky: nswe
option: ('-focuscolor', '-focusthickness')
element: Menubutton.indicator
sticky:
side: right
option: ('-arrowsize', '-arrowcolor', '-arrowpadding')
element: Menubutton.padding
expand: 1
sticky: we
option: ('-padding', '-relief', '-shiftrelief')
element: Menubutton.label
sticky:
side: left
option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')
注意到边框元素的添加了吗?
如果您尝试将 clam 主题选项菜单的布局复制到另一个主题,请使用:
newlayout = [('Menubutton.border', {'children': [('Menubutton.focus', {'children': [('Menubutton.indicator', {'sticky': '', 'side': 'right'}), ('Menubutton.padding', {'sticky': 'we', 'expand': '1', 'children': [('Menubutton.label', {'sticky': '', 'side': 'left'})]})], 'sticky': 'nswe'})], 'sticky': 'nswe'})]
s.layout(style, newlayout)
结果是:
element: TMenubutton
option: ()
element: Menubutton.border
sticky: nswe
option: ('-relief',)
element: Menubutton.focus
sticky: nswe
option: ()
element: Menubutton.indicator
sticky:
side: right
option: ('-direction', '-arrowsize', '-arrowcolor')
element: Menubutton.padding
sticky: we
expand: 1
option: ('-padding', '-relief', '-shiftrelief')
element: Menubutton.label
sticky:
side: left
option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')
不再有实际设置边框的选项。本质上,主题引擎无法正确处理具有不同元素的布局。所以您需要选择一个主题,其中包含您想要设置样式的所有元素的小部件。
在尝试制作入口框架时,我 运行 遇到了一个问题,我无法在 ttk.OptionMenu 周围制作边框以使其看起来与 ttk.Entry 相似。 (图中相邻的两个)
制作选项菜单
option = ttk.OptionMenu(bottom_container, self.have, 'ANY', 'ANY', '0', '1', style='vista.TMenubutton')
option.grid(column=1, row=2, sticky='we')
我尝试使用样式(仍想使用 vista/winnative 外观)并且能够将选项菜单背景设为白色,但我找不到适合它周围边框的方法
这是我用来检查 ttk 小部件的代码,目的是弄清楚如何为它们设置主题:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
var = tk.StringVar()
widget = ttk.OptionMenu(root, var, 'ANY', 'ANY', '0', '1')
widget.grid(column=2, row=1, sticky='nesw')
style = widget.winfo_class()
s = ttk.Style()
#s.theme_use('clam')
elements = s.layout(style)
def get_element_details(elem, _dict, depth=1):
print('%selement: %s' % (''.join(['\t' for s in range(depth)]), elem))
for key in _dict:
if key != 'children':
print('%s%s: %s' % (''.join(['\t' for s in range(depth+1)]), key, _dict[key]))
print('%soption: %s' % (''.join(['\t' for s in range(depth+1)]), s.element_options(elem)))
if 'children' in _dict:
for child, child_dict in _dict['children']:
get_element_details(child, child_dict, depth+1)
print('element: %s' % style)
print('option: %s' % str(s.element_options(style)))
for elem, elem_dict in elements:
get_element_details(elem, elem_dict)
root.mainloop()
简单地说,如果不将主题切换为 clam 主题之类的东西,小部件中没有任何选项可以添加边框(我知道这很愚蠢)
我没有要测试的 vista 主题,但是我得到了 XP 主题:
element: TMenubutton
option: ()
element: Menubutton.dropdown
side: right
sticky: ns
option: ()
element: Menubutton.button
expand: 1
sticky: nswe
option: ()
element: Menubutton.padding
expand: 1
sticky: we
option: ('-padding', '-relief', '-shiftrelief')
element: Menubutton.label
sticky:
option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')
和蛤蜊主题:
element: TMenubutton
option: ()
element: Menubutton.border
sticky: nswe
option: ('-bordercolor', '-lightcolor', '-darkcolor', '-relief', '-borderwidth')
element: Menubutton.focus
sticky: nswe
option: ('-focuscolor', '-focusthickness')
element: Menubutton.indicator
sticky:
side: right
option: ('-arrowsize', '-arrowcolor', '-arrowpadding')
element: Menubutton.padding
expand: 1
sticky: we
option: ('-padding', '-relief', '-shiftrelief')
element: Menubutton.label
sticky:
side: left
option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')
注意到边框元素的添加了吗?
如果您尝试将 clam 主题选项菜单的布局复制到另一个主题,请使用:
newlayout = [('Menubutton.border', {'children': [('Menubutton.focus', {'children': [('Menubutton.indicator', {'sticky': '', 'side': 'right'}), ('Menubutton.padding', {'sticky': 'we', 'expand': '1', 'children': [('Menubutton.label', {'sticky': '', 'side': 'left'})]})], 'sticky': 'nswe'})], 'sticky': 'nswe'})]
s.layout(style, newlayout)
结果是:
element: TMenubutton
option: ()
element: Menubutton.border
sticky: nswe
option: ('-relief',)
element: Menubutton.focus
sticky: nswe
option: ()
element: Menubutton.indicator
sticky:
side: right
option: ('-direction', '-arrowsize', '-arrowcolor')
element: Menubutton.padding
sticky: we
expand: 1
option: ('-padding', '-relief', '-shiftrelief')
element: Menubutton.label
sticky:
side: left
option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background')
不再有实际设置边框的选项。本质上,主题引擎无法正确处理具有不同元素的布局。所以您需要选择一个主题,其中包含您想要设置样式的所有元素的小部件。