我试图通过“按下”功能来改变按钮的不透明性。例如,在下面的kv文件代码中,我想通过按按钮A来尝试按钮(bt1)的不透明性。 因此,按下按钮A,它应该检查(bt1)的不透明度是否等于0,如果为真,则将其更改为1;如果为false,则应将(bt2)的不透明度从0更改为1。你知道怎么做吗?提前谢谢。在
FloatLayout:
size_hint: None, None
Button:
id: bt1
pos: 200, 300
opacity: 0
on_press: self.opacity = 0
Button:
id: bt2
pos: 300, 300
opacity: 0
on_press: self.opacity = 0
Button:
id: bt3
pos: 400, 300
opacity: 0
on_press: self.opacity = 0
Button:
pos: 0, 0
text: 'A'
on_press:
bt3.opacity = 1 if bt2.opacity == 1 else 0
bt2.opacity = 1 if bt1.opacity == 1 else 0
bt1.opacity = 1 if bt1.opacity == 0 else 1
Button:
pos: 100, 0
text: 'B'
on_press:
bt3.opacity = 1 if bt2.opacity == 1 else 0
bt2.opacity = 1 if bt1.opacity == 1 else 0
bt1.opacity = 1 if bt1.opacity == 0 else 1
Button:
pos: 200, 0
text: 'C'
on_press:
bt3.opacity = 1 if bt2.opacity == 1 else 0
bt2.opacity = 1 if bt1.opacity == 1 else 0
bt1.opacity = 1 if bt1.opacity == 0 else 1
解决方案是使用
if...elif...
。在解决方案-更改按钮的文本
按下按钮A时,检查bt1的文本是否为空字符串。如果是真的,那么将bt1的文本改为'A'。按下按钮B时,检查bt2的文本是否为空字符串。如果为真,则将bt2的文本更改为“B”。在
片段
示例-更改按钮的文本
在主.py
^{pr2}$输出-更改按钮的文本
解决方案-更改按钮的不透明度
按下按钮A时,检查bt1的不透明度是否等于0。如果是真的,那么将其更改为1。如果为false,则将bt2的不透明度从0更改为1。在
片段
示例-更改按钮的不透明度
在主.py
输出解决方案1
相关问题 更多 >
编程相关推荐