tkinter ttk Entry widget -disabledforeground
Asked Answered
O

2

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?

Orme answered 20/11, 2012 at 11:45 Comment(0)
M
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()
Miry answered 20/11, 2012 at 13:15 Comment(4)
oops!sorry, my bad, forgot to change the state, it works now!Orme
but whats the difference then? between configure and map?Orme
The main difference in this case seems to be that map avoids using disabledforeground, which doesn't appear to be recognized.Miry
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.Imprecate
B
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.

Botticelli answered 27/7, 2013 at 22:54 Comment(0)
M
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()
Miry answered 20/11, 2012 at 13:15 Comment(4)
oops!sorry, my bad, forgot to change the state, it works now!Orme
but whats the difference then? between configure and map?Orme
The main difference in this case seems to be that map avoids using disabledforeground, which doesn't appear to be recognized.Miry
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.Imprecate

© 2022 - 2024 — McMap. All rights reserved.