【发布时间】:2019-10-02 21:51:40
【问题描述】:
我希望我的按钮在按下时从蓝色变为绿色。然后,如果再次按下它,我希望它从绿色变回蓝色。我知道如何在按下按钮时更改按钮的颜色,但如果再次按下按钮,我不知道如何将该颜色更改回原始颜色。
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),即没有按下,它会变成绿色,如果不是蓝色(所以必须是绿色,它会变成蓝色。但是当我按下按钮时没有任何反应,没有错误,它只是没有变成绿色。
【问题讨论】:
标签: python python-3.x kivy kivy-language