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
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.
there aren't that many built in styles for ttk, i believe its only one style per type of widget. what ttk allows you to do is define your own styles that are derived from the builtins, tweak the bits you want different and then apply that style to all widgets easily.
This is a good reference to look at. but say for instance you wanted to change all ttk buttons so they used a red font you would do the following:
import tkinter as tk
from tkinter import ttk
from tkinter.ttk import *
root = tk.Tk()
style = ttk.Style()
style.configure("TButton", foreground="red")
btn = ttk.Button(text="Test")
btn.pack()
root.mainloop()
then every ttk button would be styled with a red foreground.
to create a new style you would do this:
import tkinter as tk
from tkinter import ttk
from tkinter.ttk import *
root = tk.Tk()
style = ttk.Style()
style.configure("Mine.TButton", foreground="red")
btn = ttk.Button(text="Test")
btn.pack()
btn2 = ttk.Button(text="Test", style="Mine.TButton")
btn2.pack()
root.mainloop()
so the first button uses the default button style, but i am deriving a new button style called Mine.TButton (the .TButton means it copies the existing style of TButton and then changes what you specified in the configure call)
so any button created with that style will look the same.
from ttkthemes import themed_tk as tk #For set Themes
from tkinter import ttk #ttk = themed tkinter
#Creating themed tkinter window and saving it in root variable
root = tk.ThemedTk()
# Get a list of all themes that can be set
root.get_themes()
#Setting theme named radiance
root.set_theme("radiance")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With