Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

python - ttk styling "TNotebook.Tab" background and borderwidth not working

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...