.kv 文件中的 RGB 和条件指令

RGB and conditional instruction in .kv file

所以我一直在开发一个应用程序,并尝试在 kv 中使用条件 kv 指令,如 here 所述。

<myWidget@Widget>:
    size: self.parent.width-self.height*0.5, self.parent.height/12
    selected: False
    canvas:
        Color: 
            rgb: 1,1,0 if self.selected else 1,1,1
        Rectangle:
            pos: self.pos
            size: self.parent.width-self.height*0.5, self.parent.height/12

我的问题是我不了解 rgb 行为:虽然此示例中的颜色工作正常(选中时变为黄色,未选中时变回白色),其他颜色组合将产生不可预知的颜色,或者没有完全没有结果。

有人可以向我解释一下 rgb 属性 到底发生了什么吗?

这被解析为 (1, 1, (0 if self.selected else 1), 1, 1),这显然不是您想要的,但(正如您所注意到的)恰好适用于 yellow/white。试试看

rgb: (1,1,0) if self.selected else (1,1,1)