1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jonathan Pasa Gu
  • 137
  • 1
  • 2
  • 7

2 Answers2

3

I think that disabledforeground is an option for tk widgets but not for ttk widgets. On this page

http://wiki.tcl.tk/38127

you will see things like this in parts of the code that apply to tk widgets:

{-disabledforeground disabledForeground}

Here -disabledforeground refers to a configuration option and disabledforeground (without the leading minus sign) refers to a color that is defined near the bottom of the page:

set colors(disabledForeground) {#a3a3a3} ; # -disabledfg

You will see things like this in parts of the code that apply to ttk widgets:

{map -foreground disabled disabledForeground}

Here -foreground is the configuration option, and disabled is a state that the widget can be in. Again, disabledforeground is the actual color to be used when the widget is in that state.

I'm not a Tcl programmer, so I'm extrapolating here from tkinter and ttk, but this is the only reasonable interpretation of this code that I can come up with.

1

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()
Junuxx
  • 14,011
  • 5
  • 41
  • 71
  • oops!sorry, my bad, forgot to change the state, it works now! – Jonathan Pasa Gu Nov 20 '12 at 13:48
  • but whats the difference then? between configure and map? – Jonathan Pasa Gu Nov 20 '12 at 13:48
  • The main difference in this case seems to be that map avoids using `disabledforeground`, which doesn't appear to be recognized. – Junuxx Nov 20 '12 at 14:15
  • Is it possible to change the colour without mapping it to a state? I want to change the colour to something different than the default if the user submits a string that is incorrect according to some criterion/ia. Since "error" state is not available (perhaps one can define a new state?) I'm looking for a way similar to `tk` has namely changing the value of "background" key to something else. – rbaleksandar Aug 13 '17 at 21:58