Home > Mobile >  tkinter styling with state map: How to set the default text color in a button?
tkinter styling with state map: How to set the default text color in a button?

Time:11-16

I am trying to style the text of a button using a style map but cannot figure out how to set the default color. I know I have to use the different states and I can change the text color if the button is pressed or disabled but I cannot find the state name for the default.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
style = ttk.Style()

style.map('TButton', 
        foreground=[
            ('disabled', 'yellow'),
            ('pressed', 'red'), 
            ('active', 'blue')
        ]
)
ttk.Button(root, text = 'Button').pack(pady = 10)
root.mainloop()

CodePudding user response:

According to docs:https://docs.python.org/3/library/tkinter.ttk.html#widget-states

There are 9 different states. active disabled focus pressed selected readonly alternate background invalid

You can set a default color by simply

style.configure("TButton",foreground="pink") 

This set a text color for your widget. Whenever it enters a special state like selected or you set widget readonly with code etc. it applies your state values.

  • Related