我希望按钮在按下时从蓝色变为绿色。如果再次按下它,我希望它从绿色变回蓝色。我知道如何在按下按钮时更改按钮的颜色,但是如果再次按下按钮,我不知道如何将其更改为原始颜色。
kv文件:
<Type>:
name: "type"
RoundedButton:
size_hint: 0.417, 0.15625
pos_hint: {"x": 0.0556, "y": 0.15}
on_press: root.change_color()
Image:
source: 'Job.PNG'
size: self.parent.width, .85 * self.parent.height
pos: self.parent.x, self.parent.y + 5
stretch: True
keep_ratio: False
<RoundedButton@Button>:
background_normal: ""
background_color: 0, 0, 0, 0
back_color: 0.2,0.6,1,1
border_radius: 10
color: self.back_color
bold: True
canvas.before:
Color:
rgba: self.back_color
Line:
rounded_rectangle: self.x, self.y, self.width, self.height, self.border_radius
width:
python文件:
class Type(Screen):
back_color = ObjectProperty()
def change_color(self):
if self.back_color == (0.2,0.6,1,1):
self.back_color = (0, 1, 0, 1)
else:
self.back_color = (0.2,0.6,1,1)
我以为我在py文件中使用的逻辑会检查颜色是否为蓝色(0.2,0.6,1,1),即未按下,它将变为绿色,如果不是蓝色(因此必须为绿色,它会变成蓝色。但是当我按下按钮时,什么也没有发生,没有错误,只是没有变成绿色。