5

I have a ttk Entry which is in "disabled" state. The background color of the entry field while disabled is a light blue shade. How can i change it to the default grey color? From this post i understood how we can change the foreground color. tkinter ttk Entry widget -disabledforeground

I tried the same method for background color and it did not work. I am using python 2.7 in Windows 7.

This is the code i tried as per the above said post:

from Tkinter import *
from ttk import *

root=Tk()

style=Style()
style.map("TEntry",background=[("active", "black"), ("disabled", "red")])
entry_var=StringVar()
entry=Entry(root,textvariable=entry_var,state='disabled')
entry.pack()
entry_var.set('test')

root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
newbieuser
  • 81
  • 1
  • 2
  • 9

3 Answers3

6

You don't need to use styles. You can change the color of disabled entry with option disabledbackground=<color>. You can use this option when creating the entry , like:

entry.config(background="black",disabledbackground="red")

So you overall code(Example) is:

from tkinter import *
import time
root=Tk()
entry=Entry(root,state='disabled')
entry.config(background="black",disabledbackground="red")
entry.pack()
root.mainloop()

Here is a screenshot of the GUI:

enter image description here

Nouman
  • 6,947
  • 7
  • 32
  • 60
2

In ttk and Tk entries widgets, background refers to different things. In Tk Entry, background refers to the color behind the text, in ttk entry, background refers to the color behind the widget. (Yes, I know, confusing right?), what you want to change is fieldbackground. So your code would be

from Tkinter import *
from ttk import *

root=Tk()

style=Style()
style.map("TEntry",fieldbackground=[("active", "black"), ("disabled", "red")])
entry_var=StringVar()
entry=Entry(root,textvariable=entry_var,state='disabled')
entry.pack()
entry_var.set('test')

root.mainloop()
Noel Segura Meraz
  • 2,265
  • 1
  • 12
  • 17
  • i saw this : http://wiki.tcl.tk/37973. and tried both `background` and `fieldbackground` as you suggested. Both are not working for me – newbieuser Dec 28 '15 at 16:37
  • @newbieuser That is strange it works for me as it is. I'm using python 2.7. Does using the Tkinter Entry widget works for you? – Noel Segura Meraz Dec 28 '15 at 16:42
  • @newbieuser `entry=Entry(root,textvariable=entry_var,state='disabled') entry.configure(disabledbackground='red')` without the `import ttk` – Noel Segura Meraz Dec 28 '15 at 16:47
  • `print(Tkinter.__version__)` $Revision: 81008 $ `print ttk.__version__` 0.3.1 – Noel Segura Meraz Dec 28 '15 at 16:51
  • yes it is working for Tkinter Entry using `disabledbackground` – newbieuser Dec 28 '15 at 17:08
  • @newbieuser then I'm really at lost here. I'm sorry I cannot offer more assistance – Noel Segura Meraz Dec 28 '15 at 17:13
  • 2
    [This answer](https://stackoverflow.com/a/17639955/806690) suggests that `fieldbackground` is not supported by the Windows style. – tm1 Aug 14 '18 at 13:04
  • I upvoted this, it works for me. For Python3 I had to make a few changes: lowercase tkinter in import // from tkinter import ttk // add ttk to Style -> ttk.Style() // entry=ttk.Entry. I didn't it get to work with plain " from ttk import * ". – JC Cheloven May 06 '21 at 11:18
-1

The following code worked for me (but it is with Tk and not with ttk):

import tkinter as tk

root = tk.Tk()

# Change the default value of disabledbackground for Entry widgets
root.option_add("*Entry.disabledBackground", "lightgray")

# Create a disabled Entry widget
disabled_entry = tk.Entry(root, state="disabled")
disabled_entry.pack()

root.mainloop()