ttk 样式“TNotebook.Tab"背景和边框不工作 [英] ttk styling "TNotebook.Tab" background and borderwidth not working

查看:77
本文介绍了ttk 样式“TNotebook.Tab"背景和边框不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩 TKinter,试图创建一个多标签窗口.

I been playing around with TKinter trying to create a multiple tab window.

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

When I try to style the TNotebook.Tab it ignores the options background and borderwidth, but it acknowledges foreground. What am I doing wrong?

这是代码的相关部分:

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)

这是窗口外观的图像:

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

In case it matters, I'm running python 2.7 on Windows 7 64-bit.

推荐答案

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

The point of the ttk theming support is to get the system provided theming engine to draw the various elements that make up a Tk widget so that we match the current UI's look and feel. In the case of the Notebook Tab on Windows Vista or above that means the 'vsapi' engine is being used to draw the elements. The tab is an element and it's look is provided by the Windows theme. For this reason it doesn't provide any way to modify the background as that is done by selecting an alternative Windows theme.

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

However, what you can do is select an element from a different ttk theme that does support changing the background style. This may not match closely in other ways but you are already diverging from the system provided theme. The way to completely control the look of your element is to create a new element using the 'image' engine and provide images to be used to paint the element. A simpler method is to borrow an element from one of the other themes. The 'default' theme provides support for specifying the color of the tab element so we could borrow that by recreating the element and redefining the TNotebook layout to use the new element.

# 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 小部件是如何设计的,要做什么并不是很明显.设计意图是您不要进行如此极端的定制,而是让您的应用程序符合用户选择的平台外观和感觉.但是工具是可用的——只是埋得很深.这是 Tcl 示例的另一个链接,该示例将 关闭按钮添加到选项卡

The options made available depend on the theming engine in use to some extent and the Windows theme is more restrictive than most because the drawing of the elements is handed over to a third party. Extreme customisation is possible but does require redefining the widget layout. This is mentioned in the python ttk docs but unless you know something about how ttk widgets are designed it's not that obvious what to do. The design intent is that you don't make such extreme customization but make your application conform to the users selected platform look and feel. But the tools are available - just buried quite deeply. Here is another link to a Tcl example that adds close buttons to the tabs

这篇关于ttk 样式“TNotebook.Tab"背景和边框不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆