去掉 Ttk Notebook 标签的虚线
我正在尝试制作一个看起来不像tkinter应用的程序。我使用了ttk Notebook,但当选中标签时,文本周围会出现一条小小的虚线。这看起来很糟糕,我找不到方法通过样式或配置来去掉它。这里有一张截图来说明:
编辑一下代码(我觉得这可能没什么帮助,因为我其实只是想去掉一个默认的样式):
这是创建笔记本的代码:
tabs = ttk.Notebook(mainframe, width=319, height=210, style=style.Notebook)
tabs.grid(column=0, row=1, sticky=('n', 'w', 'e', 's'))
tabs.columnconfigure(0, weight=1)
tabs.rowconfigure(0, weight=1)
填充内容的代码:
tab1 = ttk.Frame(tabs)
tab1_frame = ttk.Frame(tab1, style=style.Frame)
tab1_frame.pack(anchor='center', expand=1, fill='both')
# stick some widgets in
progress = ttk.Progressbar(tab1_frame, orient="horizontal", length=300, mode="determinate")
progress.grid(column=1, row=1, columnspan=2, padx=style.padding, pady=style.padding)
progress['maximum'] = 1000
progress['value'] = 500
# More widgets
# Another tab
tab2 = ttk.Frame(tabs)
tab2_frame = ttk.Frame(tab2, style=style.Frame)
tab2_frame.pack(anchor='center', expand=1, fill='both')
# blah blah
相关的样式代码:
style_config = Style()
style_config.theme_use('default')
style_config.configure(self.Notebook,
background=self.dark,
borderwidth=0)
style_config.configure(self.Tab,
background=self.dark,
foreground='white',
padding=self.padding,
borderwidth=0)
style_config.map(self.Tab,
background=[('selected', self.color1)])
3 个回答
1
你可以通过使用 theme_create()
来改变焦点颜色:
wthm = ttk.Style()
wthm.theme_create('wtheme', parent='default', settings={
'TNotebook.Tab': {
'configure': {'focuscolor':{
'configure':{
'.':'<your_color>'}
}}
})
wthm.theme_use('wtheme')
2
在Windows电脑上,如果我创建一个主题并把“经典”作为父主题,那么那个难看的虚线边框就不会出现了。
style.theme_create( "Florina", parent="classic", settings={
"TLabel": {"configure": {"background": BACKGROUND }},
"TFrame": {"configure": {"background": BACKGROUND }},
"TNotebook": {
"configure": {"background": BACKGROUND, "tabmargins": [1, 5, 2, 0] }},
"TNotebook.Tab": {
"configure": {"background": DARKBG, "padding": [5, 2] },
"map": {"background": [("selected", BACKGROUND)],
"expand": [("selected", [1, 1, 1, 0])]
} } } )
14
你可以通过改变选项卡小部件的子元素来去掉这个焦点标记。Ttk小部件是由子元素组成的。这些元素的布局是通过layout
方法来描述的(或者在theme_create
的布局参数中)。下面是一个命令,可以用来去掉布局标记(你可以直接应用到选项卡,或者其他任何派生主题上),注释部分是之前导致显示焦点的内容(通过style.layout("Tab")
获取)
style.layout("Tab",
[('Notebook.tab', {'sticky': 'nswe', 'children':
[('Notebook.padding', {'side': 'top', 'sticky': 'nswe', 'children':
#[('Notebook.focus', {'side': 'top', 'sticky': 'nswe', 'children':
[('Notebook.label', {'side': 'top', 'sticky': ''})],
#})],
})],
})]
)
还有一种比较“黑科技”的方法,就是改变这个焦点标记的颜色,比如把它改成和背景一样的颜色
style.configure("Tab", focuscolor=style.configure(".")["background"])