Change text color of an Entry () disabled

0

I was discovering a bit more of tkinter and it gives that, doing a Entry , I wanted it to be deactivated but with the text of a certain color.

I've tried it with the fg= attribute but it does not work. I tried to report on how to do it but I can not find anything.

    
asked by Esteban 24.06.2018 в 17:58
source

1 answer

1

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

    
answered by 24.06.2018 в 20:54