How many/What are the different types of ttk styles available?

Inkblot picture Inkblot · Oct 25, 2015 · Viewed 11.7k times · Source

I've been making some GUIs recently and found the buttons looked rather bland so I read some tutorials on making the look better using ttk and got the following:

from tkinter import ttk
from tkinter.ttk import *

root = tkinter.Tk()

style = ttk.Style()
style.configure("BW.TLabel")

btn = ttk.Button(text="Test")
btn.pack()

root.mainloop()

Yes it looks better but I was wondering if there are different ttk styles and if so, how many are there and where to check for them

Answer

patthoyts picture patthoyts · Oct 27, 2015

Firstly, some terminology. Ttk is a themeing layer for Tk. It consists of a collection of themes which are made up of styles that are applied to widgets. The styles are made up of elements provided by one or more element engines.

The intent of the themeing layer was to make Tk fit better into the native look and feel of the users desktop rather than permit the programmer to create all kinds of horrible looking custom widgets. That is already achieved by Tk.

So on Windows the default theme is one that can make use of the Visual Styles API. Thats the thing that draws the button edges and backgrounds and so on on Windows XP and up. As Tk supported older versions of Windows when this was created we also have a theme that supports GDI drawing elements (winnative). For Tk running on MacOS there is an element engine that uses the native themeing and on X11 we have to use Tk drawing to issue something that looks ok. The attempts to have Ttk hook into the GTK+ and Qt themeing APIs have failed to really take off.

So the themes available to you depend on the platform you are on. Different themes provide differing amounts of configurability. Those using elements provided by an external API (vsapi, winnative or aqua) do not really let you customize much. However, themes can also import elements from the default (Tk drawn) element engine or use images as visual elements. The image element engine allows you to completely change the look of the whole collection of styles (and thus the appearence of all widgets) if you really want to. However its rather slow.

In general, the point was to reduce the work on developers to make the applications look native and not to allow for ultimate customizability.

To find the themes available:

>>> ttk.Style().theme_names()
('clam', 'alt', 'default', 'classic')

This was on Linux. On Windows 7 there would be winnative, xpnative and vista as well. The initial theme is selected depending on the platform at runtime unless the application selects an alternate theme.

The styles defined within a theme are all linked to widgets. There is no method provided to enumerate all the styles. You could possibly iterate over all the ttk widgets and look at their winfo_class() method results as that shows the theme style in use by that widget.