Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading