Kivy技术问询:如何设置禁用状态下Button的文本颜色
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.disabledis True), useself.disabled_colorfor 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

