我试图通过" on-press"来试图抓住按钮的不透明度。功能。例如,在下面的kv文件代码中,我想通过按下按钮A来实现按钮(bt1)的不透明度。 因此,通过按下按钮A,它应检查(bt1)的不透明度是否等于0,如果是真,则将其更改为1,如果为假,则应将(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
答案 0 :(得分:1)
解决方案是使用if...elif...
。
按下按钮A时,检查bt1的文本是否为空字符串。如果是,那么将bt1的文本更改为“A”。按下按钮B时,检查bt2的文本是否为空字符串。如果是,则将bt2的文本更改为“B”。
Button:
pos: 0, 0
text: 'A'
on_press:
print("Button {} pressed".format(self.text))
print("\tlen(bt1.text)={}".format(len(bt1.text)))
# Assign Text
if len(bt1.text) == 0: bt1.text = self.text
elif len(bt2.text) == 0: bt2.text = self.text
# Assign Opacity
if bt2.opacity == 1: bt3.opacity = 1
elif bt1.opacity == 1: bt2.opacity = 1
elif bt1.opacity == 0: bt1.opacity = 1
Button:
pos: 100, 0
text: 'B'
on_press:
print("Button {} pressed".format(self.text))
print("\tlen(bt1.text)={}".format(len(bt1.text)))
# Assign Text
if len(bt1.text) == 0: bt1.text = self.text
elif len(bt2.text) == 0: bt2.text = self.text
# Assign Opacity
if bt2.opacity == 1: bt3.opacity = 1
elif bt1.opacity == 1: bt2.opacity = 1
elif bt1.opacity == 0: bt1.opacity = 1
from kivy.lang import Builder
from kivy.base import runTouchApp
runTouchApp(Builder.load_string('''
FloatLayout:
size_hint: None, None
size: 100, 100
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:
print("Button {} pressed".format(self.text))
print("\tlen(bt1.text)={}".format(len(bt1.text)))
# Assign Text
if len(bt1.text) == 0: bt1.text = self.text
elif len(bt2.text) == 0: bt2.text = self.text
# Assign Opacity
if bt2.opacity == 1: bt3.opacity = 1
elif bt1.opacity == 1: bt2.opacity = 1
elif bt1.opacity == 0: bt1.opacity = 1
Button:
pos: 100, 0
text: 'B'
on_press:
print("Button {} pressed".format(self.text))
print("\tlen(bt1.text)={}".format(len(bt1.text)))
# Assign Text
if len(bt1.text) == 0: bt1.text = self.text
elif len(bt2.text) == 0: bt2.text = self.text
# Assign Opacity
if bt2.opacity == 1: bt3.opacity = 1
elif bt1.opacity == 1: bt2.opacity = 1
elif bt1.opacity == 0: bt1.opacity = 1
Button:
pos: 200, 0
text: 'C'
on_press:
print("Button {} pressed".format(self.text))
# Assign Opacity
if bt2.opacity == 1: bt3.opacity = 1
elif bt1.opacity == 1: bt2.opacity = 1
elif bt1.opacity == 0: bt1.opacity = 1
'''))
按下按钮A时,检查bt1的不透明度是否等于0.如果是,则将其更改为1.如果为假,则将bt2的不透明度从0更改为1.
Button:
pos: 0, 0
text: 'A'
on_press:
if bt1.opacity == 0: bt1.opacity = 1
elif bt1.opacity == 1: bt2.opacity = 1
from kivy.lang import Builder
from kivy.base import runTouchApp
runTouchApp(Builder.load_string('''
FloatLayout:
size_hint: None, None
size: 100, 100
Button:
id: bt1
text: 'bt1'
pos: 200, 300
opacity: 0
on_press: self.opacity = 0
Button:
id: bt2
text: 'bt2'
pos: 300, 300
opacity: 0
on_press: self.opacity = 0
Button:
pos: 0, 0
text: 'A'
on_press:
if bt1.opacity == 0: bt1.opacity = 1
elif bt1.opacity == 1: bt2.opacity = 1
'''))