Search code examples
pythontkinterttk

Tkinter ttk see custom theme settings


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


Solution

  • Offical list of all options by ttk

    finally found a list that includes all coloration options to style with ttk. https://wiki.tcl-lang.org/page/Changing+Widget+Colors


    ttk.Button

    ttk::style configure TButton -background color
    ttk::style configure TButton -foreground color
    ttk::style configure TButton -font namedfont
    ttk::style configure TButton -focuscolor color
    ttk::style map TButton -background \
        [list active color disabled color readonly color]
    ttk::style map TButton -foreground \
        [list active color disabled color readonly color]
    
    ttk::style configure TButton -bordercolor color
    ttk::style configure TButton -lightcolor color
    ttk::style configure TButton -darkcolor color
    

    ttk.Checkbutton

    ttk::style configure TCheckbutton -background color
    ttk::style configure TCheckbutton -foreground color
    ttk::style configure TCheckbutton -font namedfont
    ttk::style map TCheckbutton -background \
        [list active color disabled color readonly color]
    ttk::style map TCheckbutton -foreground \
        [list active color disabled color readonly color]
    ttk::style configure TCheckbutton -indicatorcolor color
    ttk::style map TCheckbutton -indicatorcolor \
        [list selected color pressed color]
    ttk::style configure TCheckbutton -indicatorrelief relief
    ttk::style configure TCheckbutton -indicatormargin padding
    ttk::style configure TCheckbutton -indicatordiameter size
    ttk::style configure TCheckbutton -borderwidth size
    ttk::style configure TCheckbutton -focuscolor color
    

    ttk.Combobox

    ttk::style configure TCombobox -background color
    ttk::style configure TCombobox -foreground color
    ttk::style configure TCombobox -fieldbackground color
    ttk::style configure TCombobox -darkcolor color
    ttk::style configure TCombobox -lightcolor color
    ttk::style configure TCombobox -selectbackground color
    ttk::style configure TCombobox -selectforeground color
    ttk::style configure TCombobox -bordercolor color
    ttk::style configure TCombobox -insertcolor color
    ttk::style configure TCombobox -insertwidth color
    ttk::style configure TCombobox -arrowsize size
    ttk::style configure TCombobox -arrowcolor color
    ttk::style map TCombobox -background \
        [list disabled color readonly color]
    ttk::style map TCombobox -foreground \
        [list disabled color readonly color]
    ttk::style map TCombobox -fieldbackground \
        [list disabled color readonly color]
    option add *TCombobox*Listbox.background color
    option add *TCombobox*Listbox.foreground color
    option add *TCombobox*Listbox.selectBackground color
    option add *TCombobox*Listbox.selectForeground color
    

    ttk.Entry

    ttk::style configure TEntry -background color
    ttk::style configure TEntry -foreground color
    ttk::style configure TEntry -fieldbackground color
    ttk::style configure TEntry -selectbackground color
    ttk::style configure TEntry -selectforeground color
    ttk::style configure TEntry -bordercolor color
    ttk::style configure TEntry -lightcolor color
    ttk::style configure TEntry -insertcolor color
    ttk::style configure TEntry -insertwidth color
    ttk::style map TEntry -background \
        [list disabled color readonly color]
    ttk::style map TEntry -foreground \
        [list disabled color readonly color]
    ttk::style map TEntry -fieldbackground \
        [list disabled color readonly color]
    .entry configure -font namedfont
    

    ttk.Labelframe

    ttk::style configure TLabelframe -background color
    ttk::style configure TLabelframe -bordercolor color
    ttk::style configure TLabelframe -lightcolor color
    ttk::style configure TLabelframe -darkcolor color
    ttk::style configure TLabelframe.Label -background color
    ttk::style configure TLabelframe.Label -foreground color
    ttk::style configure TLabelframe.Label -font namedfont
    

    ttk.Listbox

    .listbox configure -background color
    .listbox configure -foreground color
    .listbox configure -disabledforeground color
    .listbox configure -selectbackground color
    .listbox configure -selectforeground color
    .listbox configure -font namedfont
    .listbox configure -borderwidth size
    .listbox configure -relief relief
    .listbox configure -highlightthickness size
    .listbox configure -highlightcolor color
    .listbox configure -highlightbackground color
    

    menu

    .menu configure -background color
    .menu configure -foreground color
    .menu configure -activebackground color
    .menu configure -activeforeground color
    .menu configure -disabledforeground color
    .menu configure -font namedfont
    .menu configure -selectcolor color
    .menu configure -activeborderwidth size
    .menu configure -relief relief
    

    ttk.Menubutton

    ttk::style configure TMenubutton -background color
    ttk::style configure TMenubutton -foreground color
    ttk::style configure TMenubutton -font namedfont
    ttk::style configure TMenubutton -arrowcolor color
    ttk::style map TMenubutton -background \
        [list active color disabled color]
    ttk::style map TMenubutton -foreground \
        [list active color disabled color]
    ttk::style map TMenubutton -arrowcolor \
        [list active color disabled color]
    

    ttk.Notebook

    ttk::style configure TNotebook -background color
    ttk::style configure TNotebook -bordercolor color
    ttk::style configure TNotebook -darkcolor color
    ttk::style configure TNotebook -lightcolor color
    ttk::style configure TNotebook.Tab -background color
    ttk::style configure TNotebook.Tab -foreground color
    ttk::style configure TNotebook.Tab -bordercolor color
    ttk::style configure TNotebook -focuscolor color
    ttk::style configure TNotebook -focusthickness size
    
    ttk::style configure TNotebook.Tab -focuscolor color
    ttk::style map TNotebook.Tab -background \
        [list selected color active color disabled color]
    ttk::style map TNotebook.Tab -foreground \
        [list selected color active color disabled color]
    ttk::style map TNotebook.Tab -lightcolor \
        [list selected color {} color]
    ttk::style configure TNotebook.Tab -font namedfont
    ttk::style map TNotebook.Tab -font \
        [list selected namedfont active namedfont disabled namedfont]
    

    ttk.Panedwindow

    ttk::style configure TPanedwindow -background color
    
    ttk::style configure Sash -sashthickness 5
    ttk::style configure Sash -sashrelief relief
    ttk::style configure Sash -sashpad 2
    ttk::style configure Sash -handlesize 5
    ttk::style configure Sash -handlepad 5
    
    ttk::style configure Sash -background color
    ttk::style configure Sash -lightcolor color
    ttk::style configure Sash -bordercolor color
    

    ttk.Progressbar

    ttk::style configure TProgressbar -background color
    ttk::style configure TProgressbar -troughcolor color
    ttk::style configure TProgressbar -lightcolor color
    ttk::style configure TProgressbar -darkcolor color
    ttk::style configure TProgressbar -bordercolor color
    ttk::style configure TProgressbar -barsize size
    ttk::style configure TProgressbar -pbarrelief relief
    ttk::style configure TProgressbar -borderwidth size
    

    ttk.radiobutton

    ttk::style configure TRadiobutton -background color
    ttk::style configure TRadiobutton -foreground color
    ttk::style configure TRadiobutton -font namedfont
    ttk::style map TRadiobutton -background \
        [list active color disabled color readonly color]
    ttk::style map TRadiobutton -foreground \
        [list active color disabled color readonly color]
    ttk::style configure TRadiobutton -indicatorcolor color
    ttk::style map TRadiobutton -indicatorcolor \
        [list selected color pressed color]
    

    ttk.Scale

    ttk::style configure TScale -background color
    
    ttk::style configure TScale -troughcolor color
    ttk::style map TScale -background \
        [list active color]
    ttk::style configure TScale -troughrelief relief
    
    ttk::style configure TScale -sliderrelief relief
    ttk::style configure TScale -sliderlength size
    ttk::style configure TScale -sliderthickness size
    
    ttk::style configure TScale -lightcolor color
    ttk::style configure TScale -darkcolor color
    ttk::style configure TScale -bordercolor color
    

    ttk.Scrollbar

    ttk::style configure TScrollbar -background color
    ttk::style configure TScrollbar -troughcolor color
    ttk::style configure TScrollbar -arrowcolor color
    ttk::style configure TScrollbar -bordercolor color
    ttk::style configure TScrollbar -darkcolor color
    ttk::style configure TScrollbar -lightcolor color
    ttk::style configure TScrollbar -sliderrelief relief
    ttk::style map TScrollbar -background \
        [list active color disabled color]
    ttk::style map TScrollbar -foreground \
        [list active color disabled color]
    ttk::style map TScrollbar -arrowcolor \
        [list disabled color]
    

    ttk.Seperator

    ttk::style configure TSeparator -background color
    

    ttk.Sizegrip

    ttk::style configure TSizegrip -background color
    

    ttk.Spinbox

    ttk::style configure TSpinbox -background color
    ttk::style configure TSpinbox -foreground color
    ttk::style configure TSpinbox -darkcolor color
    ttk::style configure TSpinbox -lightcolor color
    ttk::style configure TSpinbox -fieldbackground color
    ttk::style configure TSpinbox -selectbackground color
    ttk::style configure TSpinbox -selectforeground color
    ttk::style configure TSpinbox -arrowsize size
    ttk::style configure TSpinbox -arrowcolor color
    ttk::style configure TSpinbox -bordercolor color
    ttk::style configure TSpinbox -insertcolor color
    ttk::style configure TSpinbox -insertwidth color
    ttk::style map TSpinbox -background \
        [list active color disabled color readonly color]
    ttk::style map TSpinbox -foreground \
        [list active color disabled color readonly color]
    ttk::style map TSpinbox -fieldbackground \
        [list active color disabled color readonly color]
    ttk::style map TScrollbar -arrowcolor \
        [list disabled color]
    .spinbox configure -font namedfont
    

    ttk.Text

    .text configure -background color
    .text configure -foreground color
    .text configure -selectforeground color
    .text configure -selectbackground color
    .text configure -inactiveselectbackground color
    .text configure -insertbackground color
    .text configure -font namedfont
    .text configure -relief relief
    .text configure -borderwidth size
    .text configure -highlightcolor color
    .text configure -highlightthickness size
    .text configure -highlightbackground color
    

    ttk.Treeview

    ttk::style configure Treeview -background color
    ttk::style configure Treeview -foreground color
    ttk::style configure Treeview -font namedfont
    ttk::style configure Treeview -fieldbackground color
    ttk::style map Treeview -background \
        [list selected color]
    ttk::style map Treeview -foreground \
        [list selected color]
    ttk::style configure Treeview -rowheight [expr {[font metrics namedfont -linespace] + 2}]
    ttk::style configure Heading -font namedfont
    ttk::style configure Heading -background color
    ttk::style configure Heading -foreground color
    ttk::style configure Heading -padding padding
    ttk::style configure Item -foreground color 
    ttk::style configure Item -focuscolor color