【问题标题】:tkinter: button color does not updatetkinter:按钮颜色不更新
【发布时间】:2017-12-19 17:19:22
【问题描述】:

我的 GUI 的按钮 3 正在调用一个需要很长时间来计算内容的函数。所以同时我想改变按钮的文字和颜色:

self.button3.config(foreground='red')
self.button3['text'] = 'PLEASE WAIT ...'
self.button3.update_idletasks()

按钮的文本确实发生了变化,但颜色保持不变。为什么?

【问题讨论】:

    标签: python button tkinter colors updates


    【解决方案1】:

    在长时间计算期间,您的按钮可能会保持其活动状态。因此,您可能希望将其activeforeground 颜色设置为红色:

    self.button3.config(activeforeground='red')
    

    activeforeground = 按钮时使用的前景色 积极的。默认值是系统特定的。 (activeForeground/Background)

    (Tkinter Button documentation)

    【讨论】:

    • 啊,谢谢,按预期工作。但是我不能在我的 __init__ 中设置 activeforeground (按钮是类的一部分),因为这将导致按钮文本在悬停在它上面时变成红色。因此单击按钮将其设置为活动并悬停在其上。我假设一旦主循环返回,按钮就会返回到非活动状态?我仍在寻找一个很好的教程来阐明所有这些行为。非常感谢您的解决方案:)
    猜你喜欢
    • 2020-04-21
    • 2014-02-22
    • 2016-11-07
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 2016-05-26
    相关资源
    最近更新 更多