To control the color of the text and the background you have four attributes at your disposal:
-
background
or bg
: Background color when entry when in normal state. By default it is light gray.
-
foreground
or fg
: Foreground color (text) when the widget is in normal state. By default it is black.
-
disabledbackground
: Background color to show when the widget is disabled.
-
disabledforeground
: Foreground color (text) when the widget is disabled.
Therefore you must use the disabledforeground
attribute when you instantiate your Entry
(or via config
method) to specify the color of the source when it is deactivated.
A small example:
import tkinter as tk
class TestApp(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.enabled_var = tk.IntVar(value=1)
self.entry_text = tk.StringVar(value="Hola StackOverflow")
self.entry = tk.Entry(self,
background="#ccff66",
foreground="#000000",
disabledbackground="#4d4d4d",
disabledforeground="#ffffff",
textvariable=self.entry_text
)
self.check_btn = tk.Checkbutton(self,
text= "Enabled",
variable=self.enabled_var,
onvalue = 1,
offvalue = 0,
height=5,
width=20,
command=self.set_entry_state)
self.entry.pack(side=tk.LEFT, expand=True, fill="x")
self.check_btn.pack(side=tk.LEFT)
def set_entry_state(self):
if self.enabled_var.get():
self.entry.configure(state=tk.NORMAL)
else:
self.entry.configure(state=tk.DISABLED)
if __name__ == "__main__":
root = tk.Tk()
TestApp(root).pack(side="top", fill="both", expand=True)
root.mainloop()
In the following link you have the complete documentation of the widget Entry
(in English):
link