How to do it...

To add some default settings, we will simply need a ttk.Style object, which offers the following methods:

  • configure(style, opts): Changes the appearance opts for a widget style. Here is where we set options such as the foreground color, padding, and relief.
  • map(style, query): Changes the dynamic appearance for a widget style. The argument query is a keywords argument where each key is a styling option, and values are lists of tuples of the (state, value) form, meaning that the value of the option is determined by its current state.

For instance, we have marked the following examples of both situations:

import tkinter as tk
import tkinter.ttk as tk

class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Tk themed widgets")

style = ttk.Style(self)
style.configure("TLabel", padding=10)
style.map("TButton",
foreground=[("pressed", "grey"), ("active", "white")],
background=[("pressed", "white"), ("active", "grey")]
)
# ...

Now, every ttk.Label is displayed with a padding of 10, and the ttk.Button has a dynamic styling: gray foreground and white background when the state is pressed, and white foreground and gray background if the state is active.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset