python - ttk 样式 "TNotebook.Tab"背景和边框宽度不起作用

标签 python python-2.7 tkinter tk-toolkit ttk

我一直在尝试使用 TKinter 来创建一个多选项卡窗口。

当我尝试设置 TNotebook.Tab 的样式时,它忽略了选项 backgroundborderwidth,但它承认 foreground。我做错了什么?

这是代码的相关部分:

COLOR_1 = 'black'
COLOR_2 = 'white'
COLOR_3 = 'red'
COLOR_4 = '#2E2E2E'
COLOR_5 = '#8A4B08'
COLOR_6 = '#DF7401'

#Notebook Style
noteStyler = ttk.Style()
noteStyler.configure("TNotebook", background=COLOR_1, borderwidth=0)
noteStyler.configure("TNotebook.Tab", background=COLOR_1, foreground=COLOR_3, lightcolor=COLOR_6, borderwidth=0)
noteStyler.configure("TFrame", background=COLOR_1, foreground=COLOR_2, borderwidth=0)

#Create Notebook and Tabs
note = ttk.Notebook(gui, style='TNotebook')
myTab = ttk.Frame(note, style='TFrame')
note.add(myTab, text = 'MyTab', compound=tk.TOP)        
note.pack(anchor=tk.W)

这是窗口外观的图像:

enter image description here

以防万一,我在 Windows 7 64 位上运行 python 2.7。

最佳答案

ttk 主题支持的要点是让系统提供的主题引擎绘制构成 Tk 小部件的各种元素,以便我们匹配当前 UI 的外观和感觉。对于 Windows Vista 或更高版本的笔记本选项卡,这意味着“vsapi”引擎用于绘制元素。选项卡是一个元素,它的外观由 Windows 主题提供。因此,它不提供任何修改背景的方法,因为这是通过选择替代 Windows 主题完成的。

但是,您可以做的是从不同的 ttk 主题中选择一个确实支持更改背景样式的元素。这在其他方面可能不匹配,但您已经偏离了系统提供的主题。完全控制元素外观的方法是使用“图像”引擎创建一个新元素并提供用于绘制元素的图像。一种更简单的方法是从其他主题中借用一个元素。 “默认”主题支持指定选项卡元素的颜色,因此我们可以通过重新创建元素并重新定义 TNotebook 布局以使用新元素来借用它。

# Import the Notebook.tab element from the default theme
noteStyler.element_create('Plain.Notebook.tab', "from", 'default')
# Redefine the TNotebook Tab layout to use the new element
noteStyler.layout("TNotebook.Tab",
    [('Plain.Notebook.tab', {'children':
        [('Notebook.padding', {'side': 'top', 'children':
            [('Notebook.focus', {'side': 'top', 'children':
                [('Notebook.label', {'side': 'top', 'sticky': ''})],
            'sticky': 'nswe'})],
        'sticky': 'nswe'})],
    'sticky': 'nswe'})])
noteStyler.configure("TNotebook", background=COLOR_1, borderwidth=0)
noteStyler.configure("TNotebook.Tab", background="green", foreground=COLOR_3,
                                      lightcolor=COLOR_6, borderwidth=2)
noteStyler.configure("TFrame", background=COLOR_1, foreground=COLOR_2, borderwidth=0)

可用的选项在某种程度上取决于所使用的主题引擎,Windows 主题比大多数主题更具限制性,因为元素的绘制已移交给第三方。极端定制是可能的,但确实需要重新定义小部件布局。 python ttk docs 中提到了这一点但是除非您了解 ttk 小部件的设计方式,否则该怎么做并不那么明显。设计意图是您不会进行如此极端的定制,而是让您的应用程序符合用户选择的平台外观和感觉。但是工具是可用的——只是埋得很深。这是添加了 close buttons to the tabs 的 Tcl 示例的另一个链接。

关于python - ttk 样式 "TNotebook.Tab"背景和边框宽度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22389198/

相关文章:

Python\Numpy : Comparing arrays with NAN

python - BeautifulSoup Python 脚本不再适用于挖掘简单的字段

combobox - Python 2.7 : Tkinter/ttk Linked ComboBoxes

python - 格式化为整数的 numpy savetxt 不保存零

python - 使用 ElementTree 删除父标签(不删除子标签)

python - SkLearn : ValueError shapes not aligned during prediction

python : How to search a large array in effiecient way?

python - 基于规则的 ngram 映射

python - 删除 Ttk Notebook 选项卡虚线

python-3.x - 将 python 文件对话框限制为特定的文件类型