After using ttk.Style().theme_create('name', settings={})
is it possible to see the settings of that theme?
The reason I'm asking is that when I'm creating a new theme and I add ttk.Notebook(root)
to my code, the tabs have rounded corners, which I do not want.
Here is an example:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
root.title("Tab Example")
root.geometry('270x270')
background = '#ffffff'
background_dark = '#f2f2f2'
style = ttk.Style()
style.theme_create('white', settings={
'TLabel': {'configure': {'background': background}},
'TFrame': {'configure': {'background': background}},
'TNotebook': {
'configure': {'background': background_dark, 'tabmargins': [0, 7, 2, 0], 'padding': [7, 2]}},
'TNotebook.Tab': {
'configure': {'background': background_dark, 'padding': [7, 2], 'focuscolor': 'clear'},
'map': {'background': [('selected', background)]}}})
style.theme_use('white')
tab = ttk.Notebook(root)
tab1 = ttk.Frame(tab)
tab2 = ttk.Frame(tab)
tab.add(tab1, text='Tab 1')
tab.add(tab2, text='Tab 2')
tab.pack(expand=1, fill="both")
ttk.Label(tab1, text="example").pack(padx=36, pady=36)
ttk.Label(tab2, text="example 2").pack(padx=36, pady=36)
root.mainloop()
If you remove style.theme_create()
/ style.theme_use()
then the tabs no longer have rounded corners so the program must be adding that style in by default.
If there isn't a way to see the theme settings (can't seem to find it in the docs) is there a list of possible settings that I can use? Something specifically for tab borders?
On that note, there's a similar question, Is there a Tkinter/ttk style reference? however the first link in the provided answer doesn't list anything for border corners or border styles under ttk::notebook while the second link is unresponsive.
EDIT
Expanding upon Atlas435's answer,
style_name = ttk.Notebook(None).winfo_class()
# print(style_name) -> 'TNotebook'
print(style.layout('TNotebook')) # -> [('Notebook.client', {'sticky': 'nswe'})]
print(style.element_options('Notebook.client')) # -> ('borderwidth', 'background')
Except for 'background'
, I'm not able to see the names of the custom settings I used above for 'TNotebook'
:
style.theme_create('white', settings={
'TNotebook': {'configure':
{'background': background_dark, 'tabmargins': [0, 7, 2, 0], 'padding': [7, 2]}}})
If I instead do this, I get closer to what I'm looking for but still not quite:
print(style.layout('Tab')) # -> [('Notebook.tab', {'sticky': 'nswe', 'children': [('Notebook.padding', {'sticky': 'nswe', 'children': [('Notebook.label', {'sticky': 'nswe'})]})]})]
print(style.element_options('Notebook.tab')) # -> ('borderwidth', 'background')
Cycling through the other element_options
(Notebook.padding
and Notebook.label
) doesn't provide the values I'm looking for either :(
EDIT 2
Some styling options aren't listed anywhere including the Tcl/Tk docs.
An example of this is 'focuscolor'
for 'TNotebook.Tab'
which changes the color of the dashed lines around the Tab when it is in focus.
Another example is when using ttk.Style().theme_use('default')
or .theme_use('classic')
, the Tab's in Notebook have rounded edges. If you use .theme_use('clam')
or .theme_use('vista')
, the Tab's in Notebook don't have rounded edges.
I'm unable to find that style option in any documentation, and I cannot get it to print through the program (see above Edit section).
For now I'm accepting the current best answer (Atlas435) for helping me come to this conclusion.
A temporary solution for anyone else stumbling upon this could be to set either 'clam'
or 'vista'
as a parent
when using ttk.Style().theme_create()
or to create a picture that looks like a Tab with the styling you want and use tab.add(tab1, image=img)
FINAL
A full list is available, check out Atlas 435's answer