ttk styling “TNotebook.Tab” background and borderwidth not working

发布于 2021-01-29 14:09:50

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

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

Here is the relevant part of the code:

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)

Here is an image of what the window looks like:

enter image description here

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

关注者
0
被浏览
226
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    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.

    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)
    

    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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看