Next / Previous / Contents / TCC Help System / NM Tech homepage

49. Using and customizing ttk styles

Within a given theme, every widget has a default widget class; we use this term to distinguish ttk classes from Python classes.

Each widget also has a style. The default style for a widget is determined by its widget class, but you may specify a different style.

In ttk, widget classes and styles are specified as strings. In all but one case, the default style name of a widget is 'T' prefixed to the widget name; for example, the default button widget class is 'TButton'. There are some exceptions:

Table 63. Style names for ttk widget classes

Widget classStyle name
Button TButton
Checkbutton TCheckbutton
Combobox TCombobox
Entry TEntry
Frame TFrame
Label TLabel
LabelFrame TLabelFrame
Menubutton TMenubutton
Notebook TNotebook
PanedWindow TPanedwindow (not TPanedWindow!)
Progressbar Horizontal.TProgressbar or Vertical.TProgressbar, depending on the orient option.
Radiobutton TRadiobutton
Scale Horizontal.TScale or Vertical.TScale, depending on the orient option.
Scrollbar Horizontal.TScrollbar or Vertical.TScrollbar, depending on the orient option.
Separator TSeparator
Sizegrip TSizegrip
Treeview Treeview (not TTreview!)

At runtime, you can retrieve a widget's widget class by calling its .winfo_class() method.

>>> b=ttk.Button(None)
>>> b.winfo_class()
>>> t=ttk.Treeview(None)
>>> t.winfo_class()
>>> b.__class__    # Here, we are asking for the Python class
<class ttk.Button at 0x21c76d0>

The name of a style may have one of two forms.

Every style has a corresponding set of options that define its appearance. For example, buttons have a foreground option that changes the color of the button's text.

To change the appearance of a style, use its .configure() method. The first argument of this method is the name of the style you want to configure, followed by keyword arguments specifying the option names and values you want to change. For example, to make all your buttons use green text, where s is in instance of the ttk.Style class:

s.configure('TButton', foreground='green')

To create a new style based on some style oldName, first create an instance of ttk.Style, then call its .configure() method using a name of the form 'newName.oldName'. For example, suppose you don't want to use maroon text on all your buttons, but you do want to create a new style that does use maroon text, and you want to call the new style 'Kim.TButton':

s = ttk.Style()
s.configure('Kim.TButton', foreground='maroon')

Then to create a button in the new class you might use something like this:

self.b = ttk.Button(self, text='Friday', style='Kim.TButton',

You can even build entire hierarchies of styles. For example, if you configure a style named 'Panic.Kim.TButton', that style will inherit all the options from the 'Kim.TButton' style, that is, any option you don't set in the 'Panic.Kim.TButton style will be the same as that option in the 'Kim.TButton' style.

When ttk determines what value to use for an option, it looks first in the 'Panic.Kim.TButton' style; if there is no value for that option in that style, it looks in the 'Kim.TButton' style; and if that style doesn't define the option, it looks in the 'TButton' style.

There is a root style whose name is '.'. To change some feature's default appearance for every widget, you can configure this style. For example, let's suppose that you want all text to be 12-point Helvetica (unless overriden by another style or font option). This configuration would do it:

    s = ttk.Style()
    s.configure('.', font=('Helvetica', 12))