问题
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?
回答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()
回答2:
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.
来源:https://stackoverflow.com/questions/13472501/tkinter-ttk-entry-widget-disabledforeground