删除Ttk Notebook标签虚线

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了删除Ttk Notebook标签虚线相关的知识,希望对你有一定的参考价值。

我正在尝试制作一个看起来不像tkinter应用程序的tkinter应用程序。我正在使用ttk笔记本,当选中时,标签在文本周围有一条小虚线。它看起来很糟糕,我找不到使用样式或配置删除它的方法。这是一个截图,以澄清:

编辑代码(我不认为它会非常有用,因为我实际上只是想删除一个默认样式的东西。):

这是笔记本创作:

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)])
答案

您可以通过更改选项卡窗口小部件的子元素来删除此焦点标记。 Ttk小部件在subelements中被分解。这些元素的布局通过layout方法(或在theme_create的布局参数中)描述。这是一个删除布局标记的命令(您可以直接将它应用于Tab或任何其他派生主题),注释部分是先前绘制焦点的原因(通过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"])
另一答案

在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])]
                      } } } )

以上是关于删除Ttk Notebook标签虚线的主要内容,如果未能解决你的问题,请参考以下文章

如何在tkinter中并排打包3个单选按钮和1个标签?

tkinter中ttk.Notebook实现多页面切换

使用 ttk.Notebook 小部件从右到左对齐选项卡

创建新标签(ttk笔记本)时如何保留小部件?

在PaddlePaddle中的Notebook代码片段

如何在 Python ttk 中的 Button 中对齐文本