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

Kivy按钮背景色RGBA值异常:超220/255时显示默认灰色

Kivy Button设置background_color高RGB值变灰色的解决方法

问题原因

Kivy的Button默认使用自带的背景纹理(来自内置UI资源集),background_color属性本质是对这个纹理做染色叠加处理。当RGB值接近1.0(如220/255≈0.86以上)时,亮色叠加在默认灰色纹理上会被中和,最终呈现默认灰色;另外你代码里background_color的alpha值设为0,这会让设置的颜色完全透明,进一步导致默认纹理的颜色显现。

修复后的代码

修改Button配置,去掉默认背景纹理并修正alpha值:

Button:
    size_hint : 0.95/18, 0.1
    pos_hint : {"center_x" : 0.95, "center_y" : 0.925}
    font_size : 40  # 原代码的font属性错误,Kivy中需用font_size设置字号
    background_normal: ''  # 清空正常状态的默认背景纹理
    background_down: ''    # 清空按下状态的默认背景纹理
    background_color: 1.0, 225.0/255, 228.0/255, 1.0  # 将alpha设为1.0保证颜色不透明
    color: 211.0/255, 54.0/255, 112.0/255, 1
    text: "Sample Text"
    halign : "center"

关键说明

  • 清空background_normal和background_down后,Button会直接用background_color作为纯色背景,不再受默认纹理干扰,高RGB值的颜色就能正常显示。
  • background_color的第四个参数是alpha通道,设为1.0才能让颜色完全显示,设为0会导致颜色透明,默认纹理就会露出来。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 02:40:28