Search code examples
tkinterconfigurettktkinter-entry

tkinter ttk Entry widget -disabledforeground


I was trying to change the color of the words in my ttk.Entr widget when I set the state to disabled, I looked up the manual, there's an option called disabledforeground, so I wrote a test snippet as below: (BTW, I'm under Python 2.7)

from Tkinter import *
from ttk import *

root=Tk()

style=Style()
style.configure("TEntry",disabledforeground='red')

entry_var=StringVar()
entry=Entry(root,textvariable=entry_var,state='disabled')
entry.pack()

entry_var.set('test')

mainloop()

But the result shows no change in the color of "test", any idea what's wrong?


Solution

  • Try using Style.map instead of configure.

    from Tkinter import *
    from ttk import *
    
    root=Tk()
    
    style=Style()
    style.map("TEntry",
              foreground=[("active", "black"), ("disabled", "red")]
              )
    
    entry_var=StringVar()
    entry=Entry(root,textvariable=entry_var,state='disabled')
    entry.pack()
    
    entry_var.set('test')
    
    mainloop()