ttk:条目小部件禁用背景颜色

new*_*ser 3 python widget ttk

我有一个处于“禁用”状态的 ttk 条目。禁用时输入字段的背景颜色是浅蓝色阴影。如何将其更改为默认的灰色?从这篇文章中,我了解了如何更改前景色。 tkinter ttk 入口小部件 -disabledforeground

我尝试了相同的背景颜色方法,但没有奏效。我在 Windows 7 中使用 python 2.7。

这是我按照上述帖子尝试的代码:

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()
Run Code Online (Sandbox Code Playgroud)

Bla*_*der 6

您不需要使用样式。您可以使用 option 更改禁用条目的颜色disabledbackground=<color>。您可以在创建条目时使用此选项,例如:

entry.config(background="black",disabledbackground="red")
Run Code Online (Sandbox Code Playgroud)

所以你的整体代码(示例)是:

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

这是 GUI 的屏幕截图:

在此处输入图片说明


Noe*_*raz 2

在 ttk 和 Tk 条目小部件中,background指的是不同的事物。在 Tk Entry 中,background指的是文本后面的颜色,在 ttk Entry 中,background指的是 widget 后面的颜色。(是的,我知道,令人困惑吧?),你想要改变的是fieldbackground。所以你的代码是

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()
Run Code Online (Sandbox Code Playgroud)

  • [这个答案](/sf/answers/1234796881/)表明Windows风格不支持`fieldbackground`。 (2认同)