简体   繁体   中英

ttk:Entry widget disabled background color

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()

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()

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:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM