【问题标题】:ttk:Entry widget disabled background colorttk:Entry 小部件禁用背景颜色
【发布时间】:2015-12-28 13:23:19
【问题描述】:

我有一个处于“禁用”状态的 ttk 条目。 禁用时输入字段的背景颜色为浅蓝色。 如何将其更改为默认的灰色?从这篇文章中,我了解了如何更改前景色。 tkinter ttk Entry widget -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()

【问题讨论】:

    标签: python widget ttk


    【解决方案1】:

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

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

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

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

    这是图形用户界面的截图:

    【讨论】:

    • 我很抱歉,我不小心否决了这个答案。除非您编辑答案,否则我无法更改投票
    • @BryanOakley 没问题 :) 。我编辑了我的帖子。
    • 问题是关于ttk,而你用tk切换了它。
    【解决方案2】:

    在 ttk 和 Tk 条目小部件中,background 指的是不同的东西。在 Tk Entry 中,background 指的是文本后面的颜色,在 ttk entry 中,background 指的是小部件后面的颜色。 (是的,我知道,令人困惑吗?),您要更改的是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()
    

    【讨论】:

    • 我看到了这个:wiki.tcl.tk/37973。并按照您的建议尝试了backgroundfieldbackground。两者都不适合我
    • @newbieuser 这很奇怪,它对我有用。我正在使用python 2.7。使用 Tkinter Entry 小部件对您有用吗?
    • @newbieuser entry=Entry(root,textvariable=entry_var,state='disabled') entry.configure(disabledbackground='red') 没有import ttk
    • print(Tkinter.__version__) $Revision: 81008 $ print ttk.__version__ 0.3.1
    • This answer 建议 fieldbackground 不受 Windows 样式支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 2019-08-27
    相关资源
    最近更新 更多