tkinter.ttk.Style.map()

map(style, query_opt=None, **kw)

Query or sets dynamic values of the specified option(s) in style.

Each key in kw is an option and each value should be a list or a tuple (usually) containing statespecs grouped in tuples, lists, or some other preference. A statespec is a compound of one or more states and then a value.

An example may make it more understandable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import tkinter
from tkinter import ttk
 
root = tkinter.Tk()
 
style = ttk.Style()
style.map("C.TButton",
    foreground=[('pressed', 'red'), ('active', 'blue')],
    background=[('pressed', '!disabled', 'black'), ('active', 'white')]
    )
 
colored_btn = ttk.Button(text="Test", style="C.TButton").pack()
 
root.mainloop()

Note that the order of the (states, value) sequences for an option does matter, if the order is changed to [('active', 'blue'), ('pressed', 'red')] in the foreground option, for example, the result would be a blue foreground when the widget were in active or pressed states.

doc_python
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.