You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kivy技术问询:如何设置禁用状态下Button的文本颜色

Fixing Disabled Button Text Color in Kivy 1.10.1

I ran into this exact issue back when using Kivy 1.10.x—turns out, even though Button inherits from Label (which has the disabled_color property), the Button widget doesn't automatically switch its text color to disabled_color when it's disabled in older versions like 1.10.1.

The Solution: Bind the Color to the Disabled State

You need to explicitly tie the color property to the button's disabled status, so it uses disabled_color when disabled, and your normal color otherwise. Here's how to modify your KV code:

Button:
    font_size: '35sp'
    # Bind color to switch based on disabled state
    color: self.disabled_color if self.disabled else (1, 1, 1, 1)
    disabled_color: 25/255, 25/255, 25/255, 1
    # Background settings remain the same
    background_normal: 'button_normal.png'
    background_down: 'button_down.png'
    background_disabled_normal: 'button_normal_disn.png'
    background_disabled_down: 'button_normal_disd.png'

Why This Works

In Kivy 1.10.1, the Button widget doesn't inherit the automatic text color switching behavior from Label. By using a conditional expression in the color property, we're telling Kivy:

  • If the button is disabled (self.disabled is True), use self.disabled_color for the text
  • Otherwise, use the normal white color (1,1,1,1)

Note for Future Reference

If you ever upgrade to Kivy 1.11.0 or later, this behavior was adjusted—Button will now automatically use disabled_color for text when disabled, so you won't need this manual binding anymore. But for your current 1.10.1 setup, this binding is the way to go.

内容的提问来源于stack exchange,提问作者fritte2000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 20:22:47