Skip to content
Advertisement

How to configure map for all ttk widgets except TButton?

I am creating a GUI with Tkinter and ttk, and I’m trying to create a custom map to make ttk widgets blue on hover. I could apply that to all widgets with passing "." as the first argument to ttk.Style().map(...).

import tkinter as tk
import tkinter.ttk as ttk


root = tk.Tk()

style = ttk.Style()
style.map(".", background=[("active", "blue")])

button = ttk.Button(root, text="An example button")
button.pack()
scrollbar = ttk.Scrollbar(root)
scrollbar.pack()

root.mainloop()

But now I want to exclude TButton from this query. That is, I need to make all widgets but TButton blue on hover. How can I do that?

Passing “.!TButton” as well as “!TButton” instead of “.” has no effect.

Advertisement

Answer

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).

So we can override it by simply adding another style:

style.map('TButton',background=[('active','#f0f0f0')])

Keep a note that you might want to do this for every style that you wish to set to default too.

Take a read here for more info.

Advertisement